Java Enum Comparison: == vs equals Explained

How-to basics · Updated November 23, 2026

In Java, you compare enum values with the == operator, not equals(). Because enums are singletons per JVM, == is safe, faster, and null-safe, making it the idiomatic choice in virtually all cases.

Why == Works for Enums

Every enum constant in Java is a static final instance of its enum type. The JVM guarantees that each constant exists exactly once per classloader. This means two references to the same enum constant are always the same object. The == operator compares object identity, so it returns true exactly when both sides refer to the same constant.

Consider this example:

enum Color { RED, GREEN, BLUE }

Color c1 = Color.RED;
Color c2 = Color.RED;
System.out.println(c1 == c2); // true
System.out.println(c1.equals(c2)); // true

Both comparisons return true, but == does it without a method call, and it cannot be overridden. The equals() method on an enum is final and simply delegates to ==, but using it adds no benefit and risks confusion.

Null Safety: A Hidden Difference

The most practical reason to prefer == is null handling. If a variable can be null, calling equals() on it throws a NullPointerException. With ==, you can safely compare a null reference to an enum constant:

Color c = null;
if (c == Color.RED) { // false, no exception
// ...
}
if (c.equals(Color.RED)) { // NullPointerException
// ...
}

This makes == the safer default, especially when dealing with data from external sources like databases or JSON where nulls are common.

How equals() Behaves on Enums

The Enum class overrides equals() as a final method. Its implementation is simply return this == other;. Because it is final, no subclass can change it. This means equals() and == are semantically identical for enums, but equals() adds a method call overhead and the null risk mentioned above.

There is one subtle scenario: if you load the same enum class in two different classloaders, you get two distinct class objects, and constants from each are not == to each other. This is rare in standard applications (usually only in application servers or plugin systems). In such cases, even equals() fails because it also relies on identity. The only workaround is comparing name() strings, but that is fragile and should be avoided unless you truly have classloader isolation.

When to Use equals() Instead

There is no real scenario where equals() is required for enums. Some developers use it out of habit from comparing other objects. However, there is one situation where it might be preferable for consistency: if you are writing generic code that works with any object type, using equals() avoids compile-time issues. For example:

public <T> boolean isInList(T value, List<T> list) {
for (T item : list) {
if (item.equals(value)) {
return true;
}
}
return false;
}

Here, item.equals(value) works for enums, strings, and any other object. Using == would not compile for generic T. But this is a generic utility, not a direct enum comparison.

Common Mistakes and Misconceptions

A frequent mistake is using switch statements with enums and forgetting that switch internally uses == (or a lookup table) — it does not call equals(). This is fine because it works correctly. Another misconception is that enums are like integers, so comparing with == might be unsafe if someone creates a new instance via reflection. In practice, the JVM prevents creating additional instances of an enum constant through reflection (since Java 5, and more strictly since Java 9 with Class reflection checks).

Another pitfall: comparing enums across different enum types. Color.RED == Shape.CIRCLE will not compile because the types differ. Using equals() would compile but always return false, hiding a design error. The compiler catching this is a benefit of ==.

Performance and Readability

Performance-wise, == is a single JVM instruction (reference comparison), while equals() is a virtual method call that then does the same comparison. In hot loops, this difference is negligible, but it is still a tiny win for ==. More importantly, readability: experienced Java developers immediately understand if (status == Status.ACTIVE) as an enum identity check, whereas if (status.equals(Status.ACTIVE)) raises a moment of doubt about whether a custom equals() might be involved. Since enums cannot override it, the doubt is unnecessary.

Alternatives: name() and ordinal()

Sometimes developers compare enums by their name() string or ordinal() int. This is almost always wrong. name() is for persistence or display, not for logic. If you rename an enum constant, all name() comparisons break silently. ordinal() is even more fragile: it depends on declaration order. Inserting a new constant in the middle shifts all subsequent ordinals, breaking any stored values. Only use ordinal() for serialization or array indexing, and even then consider a custom field instead.

If you need to compare enums based on a business attribute, add a field to the enum:

enum Priority {
LOW(1), MEDIUM(2), HIGH(3);
private final int level;
Priority(int level) { this.level = level; }
public int getLevel() { return level; }
}

Then compare p1.getLevel() == p2.getLevel() if needed, but for equality of constants, stick with ==.

Frequently Asked Questions

Is it safe to use == for enums in a distributed system?

Yes, as long as the enum class is loaded by the same classloader on each JVM. In a distributed system, each JVM has its own copy of the enum class, but within that JVM, == works correctly. Comparing enum values across JVMs (e.g., sending them over a network) is done by serializing the name() string, not by comparing references. When you deserialize, you get the local singleton, and == works again.

What happens if I use equals() on an enum that is null?

You get a NullPointerException. This is the main reason to prefer ==. If you must use equals(), you need an explicit null check first: if (c != null && c.equals(Color.RED)). That is extra code with no benefit.

Can I compare enums with switch statements?

Yes, and it is the recommended way for branching. The switch statement on enums works by comparing the enum’s ordinal internally, but it is safe because it is generated by the compiler. You do not need to use == or equals() inside a switch; just write switch(color) { case RED: ... }. Note that you cannot use switch with null — it throws NullPointerException.

Why does my IDE suggest equals() instead of ==?

Some IDEs have a general inspection that suggests equals() for object comparisons, but they usually exclude enum types. If your IDE suggests it for an enum, it might be a misconfiguration. You can safely ignore it or disable that inspection for enums. The Java language specification and official Java tutorials explicitly recommend using == for enums.

Does using == break if I use a different JVM or Android?

No. The singleton guarantee is part of the Java language specification, and Android’s implementation follows it. As long as you are using standard Java or Android, == works. The only exception is if you use a custom classloader that loads the same enum class twice, which is an advanced scenario you are unlikely to encounter.

Should I use == or equals() when comparing enum values in a stream or lambda?

Use == in any predicate or comparator. For example, list.stream().filter(c -> c == Color.RED) is fine. There is no reason to use equals() there. If you are comparing to a variable that could be null, == handles it gracefully, while equals() would require a null check inside the lambda.

Related

Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.

More in How-to basics

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.