# Replace equals() on Enum Constants

# Description

Replaces occurrences of equals() on Enum constants with an identity comparison (==). In the case the equals relation is wrapped with an boolean negation the result will be an not equals (!=).

# Benefits

Technically, equals and == are the same. == however is null safe and thus can help avoid programming errors.

# Tags

# Code Changes

# Enum in right-hand-side

Pre

public boolean isRoundUp(RoundingMode roundingMode) {
    return roundingMode.equals(RoundingMode.UP);
}

Post

public boolean isRoundUp(RoundingMode roundingMode) {
    return roundingMode == RoundingMode.UP;
}

# Enum in left-hand-side

Pre

public boolean isRoundUp(RoundingMode roundingMode) {
    return RoundingMode.UP.equals(roundingMode);
}

Post

public boolean isRoundUp(RoundingMode roundingMode) {
    return RoundingMode.UP == roundingMode;
}

# Enum in infix expression

Pre

public boolean isRoundUp(RoundingMode roundingMode) {
    return !RoundingMode.UP.equals(roundingMode);
}

Post

public boolean isRoundUp(RoundingMode roundingMode) {
    return !(RoundingMode.UP == roundingMode);
}

Use a Java Refactoring Tool

Automate this Refactoring system-wide

You can apply this refactoring for free with the jSparrow Eclipse IDE plug-in.
Install the plug-in for Eclipse IDE: Eclipse Marketplace.

a drawn cute bird pointing at a graph that shows positive results

# Properties

Property Value
Rule ID EnumsWithoutEquals
First seen in jSparrow version 2.2.0
Minimum Java version 5
Remediation cost 5 min
Links