Quick wins for a faster PC:
Repair Windows errors before they cause bigger problemsFix Now →Scan for outdated or missing drivers - takes under a minuteDriver Scan →Use == and != to test whether a Java enum variable refers to a particular constant. They are the clearest choice for enum equality, and they remain safe when the variable is null.
Compare enum constants with == or !=
Each enum constant is a single instance. For constants of the same enum type, == is true when both references denote that same constant; != is true when they do not. The Java Language Specification, §8.9, explicitly permits using == in place of equals when comparing references if at least one refers to an enum constant.
enum State { NEW, RUNNING, DONE }
State state = State.RUNNING;
if (state == State.RUNNING) {
// handle the running state
}
if (state != State.DONE) {
// not finished yet
}
What happens if the enum variable is null?
Comparing a possibly null variable with an enum constant using == or != does not throw an exception. If state is null, state == State.RUNNING evaluates to false, while state != State.RUNNING evaluates to true.
Calling state.equals(State.RUNNING) throws NullPointerException when state is null, because the method call uses the null variable as its receiver. Calling State.RUNNING.equals(state) avoids that particular null-receiver problem, but == is the direct idiom for enum identity.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
Why == and equals agree for enums
The language specification says an enum constant has only one instance. Java’s Enum API also specifies that Enum.equals(Object) is final and returns true when the supplied object is equal to that enum constant. In practice, for enum constants, this equality is identity-based, so == and equals agree.
Use compareTo only when declaration order matters
compareTo answers an ordering question, not an equality question: it compares constants of the same enum type according to the order in which they are declared. Constants from different enum types are not comparable through compareTo. For a test such as “is this the running state?”, use ==, not an ordering comparison.
Rank #2
Avoid using ordinal() as a general-purpose business value. The Enum API says most programmers have no use for it and identifies enum-based data structures such as EnumSet and EnumMap as its intended use.
Quick Recap
Best Value
Rank #4
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

