Java Enum Identity and Equality

Java enum constants are unique instances, so comparing enum values with `==` is safe and directly tests whether they are the same constant. `equals()` also works for enum constants, but `==` is usually the clearer choice.

Each constant declared by a Java `enum` represents a distinct object instance managed by the JVM. A variable of an enum type therefore refers to one of those predefined constants rather than to an arbitrary newly created enum object.

The `==` operator compares object references for identity. For enum constants, identity corresponds to the enum constant itself, so `Color.RED == Color.RED` is true and different constants such as `Color.RED == Color.BLUE` are false.

Enum types inherit an `equals()` implementation from `java.lang.Enum` that is based on identity. Consequently, for enum constants, `==` and `equals()` produce the same equality result when both operands are non-null.

A practical advantage of `==` is its null-safe behavior: comparing an enum variable with a constant using `==` is simply false when the variable is `null`. Calling `equals()` on a null enum reference would instead throw `NullPointerException`.

This identity rule also makes enums appropriate for `switch` statements and other logic where the program needs to distinguish a fixed set of named states or categories.

Key points

Interview questions

Why shouldn't you apply the enum `==` rule to ordinary Java objects?

For ordinary objects, `==` compares whether two references point to the same object, not whether their contents are logically equal. Two separate objects can contain equivalent data while having different identities. Their class may override `equals()` to define value-based equality. Enum types are special because their declared constants have fixed, unique identities.

Why is it safe to compare Java enums with `==`?

Each enum constant is a unique instance of its enum type. The `==` operator checks reference identity, so it correctly determines whether two references refer to the same enum constant. Different constants have different identities. This makes `==` a natural way to compare enum values.

What is the difference between `==` and `equals()` for enums?

For non-null enum values, `==` and `equals()` give the same result because enum equality is based on identity. `==` directly expresses that two references point to the same constant. It also avoids a null dereference when comparing a possibly null variable to a known constant. For these reasons, `==` is generally preferred for enum comparisons.

What happens when an enum variable is null and you compare it with a constant?

An expression such as `status == Status.NEW` evaluates to false when `status` is null. Reference comparison permits null as an operand and does not dereference it. By contrast, `status.equals(Status.NEW)` attempts to invoke a method through the null reference and throws `NullPointerException`. This is one practical reason `==` is convenient for enum checks.

Would you ever use `equals()` to compare enum values?

Yes, `equals()` is valid for enum values because enum equality is identity-based. However, `==` is usually preferable because it communicates the enum identity rule explicitly and can avoid null-related problems when the variable is compared directly with a constant. The choice should also remain consistent with the surrounding codebase's conventions. If nullability is involved, the exact expression matters because invoking an instance method on a null reference is unsafe.