Introduction: A Mistake You Might Be Making Right Now
\n\nImagine this: you're writing Java code, using enums, and you need to compare two values. You automatically write if (day == Day.MONDAY). Or maybe if (day.equals(Day.MONDAY)). Which option is correct? And is there even a difference?
This question is one of the most popular on Stack Overflow. Thousands of developers (from beginners to seniors) have asked it. It seems like a small detail. But behind this "small detail" lies a deep understanding of how the JVM works, how enums are structured, and why not everything in Java is so straightforward.
\n\nIn this article, we will finally figure out: how to correctly compare enum in Java, when to use ==, when to use equals(), and why one of these options is almost always the best choice.
Short Answer for the Impatient
\n\nIf you need an answer right now — use ==.
But! Before you close the article, let's understand why this is the case, and what pitfalls might await you. Because the devil, as always, is in the details.
\n\nWhy is an enum not just a set of constants?
\n\nTo understand the difference between == and equals(), you need to remember what an enum actually is in Java.
Each enum is a full-fledged class that implicitly inherits from java.lang.Enum. Each enum constant is a static final instance of this class. The JVM guarantees that for each enum value, there is exactly one object in memory.
Look at this code:
\n\npublic enum Day {\n MONDAY,\n TUESDAY,\n WEDNESDAY,\n THURSDAY,\n FRIDAY,\n SATURDAY,\n SUNDAY\n}\n\nWhen the JVM loads this enum, it creates exactly 7 objects. Day.MONDAY is always the same object. Always. And this is the key point.
Comparison via ==: Why does it work?
\n\nThe == operator in Java compares references to objects. It doesn't look at the content — it checks whether two variables point to the same object in memory.
Since the JVM guarantees the uniqueness of each enum instance, comparing enums with == works flawlessly:
public class EnumComparison {\n public static void main(String[] args) {\n Day today = Day.WEDNESDAY;\n Day anotherDay = Day.WEDNESDAY;\n \n // Both variables refer to the same object in memory\n System.out.println(today == anotherDay); // true\n \n // Comparison with another constant\n System.out.println(today == Day.MONDAY); // false\n }\n}\n\nOutput:
\ntrue\nfalse\n\nSimple and elegant. But let's look at what happens under the hood.
\n\nWhy is it safe? JVM Guarantees
\n\nThe Java Language Specification (JLS 8.9) guarantees that each enum value is initialized exactly once when the class is loaded. No reflection, serialization, or cloning can create a duplicate. Even if you try to use new — the compiler will throw an error.
Therefore, == for enums is not just "it works", it is the fastest and most reliable way to compare them.
Comparison via equals(): It works, but there are nuances
\n\nThe equals() method also works. The Enum class overrides equals(), and internally it uses ==:
// Real code from java.lang.Enum\npublic final boolean equals(Object other) {\n return this == other;\n}\n\nThat is, calling day.equals(Day.MONDAY) ultimately does the same thing as day == Day.MONDAY. But there is an important difference:
The null problem
\n\nIf your variable can be null