How to Compare Enums in Java

How-to basics · Updated November 22, 2026

Comparing enums in Java is straightforward because every enum type implicitly extends java.lang.Enum, which provides built-in methods for equality and ordering. This guide covers the three main ways to compare enums: using ==, using equals(), and using compareTo(). You’ll also learn when each is appropriate, how to handle nulls, and how to avoid common pitfalls.

What You’ll Need

  • A Java development environment (JDK 8 or later is fine; examples work on any modern version).
  • Basic familiarity with defining and using enums in Java.

Step-by-Step

  1. Define an enum for testing. For example:

    public enum Day {
        MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
    }
  2. Compare with the == operator. This is the most common and recommended way to check if two enum variables refer to the same constant. Because enums are singletons (each constant is a single instance), == works reliably and is null-safe when you check for null first.

    Day today = Day.MONDAY;
    Day other = Day.MONDAY;
    if (today == other) {
        System.out.println("Same day");
    }
  3. Use equals() for comparison. The equals() method is inherited from Enum and behaves exactly like == (it checks reference equality). It’s safe to use, but many developers prefer == because it’s more concise and avoids a method call.

    if (today.equals(other)) {
        System.out.println("Same day");
    }

    Note: equals() will throw a NullPointerException if you call it on a null reference. Always check for null first if the variable might be null.

  4. Compare enum constants with compareTo(). Enums implement Comparable via Enum, so you can compare them by their ordinal (declaration order). This returns a negative integer, zero, or a positive integer.

    Day first = Day.MONDAY;
    Day second = Day.WEDNESDAY;
    int result = first.compareTo(second);
    // result is negative because MONDAY comes before WEDNESDAY
  5. Use ordinal() for explicit index comparison. If you need the numeric position of an enum constant, call ordinal() (0-based). Comparing ordinals is equivalent to comparing with compareTo().

    int index = Day.FRIDAY.ordinal(); // returns 4
  6. Handle nulls safely. When comparing variables that could be null, use Objects.equals() (Java 7+) or check for null explicitly.

    Day a = null;
    Day b = Day.MONDAY;
    if (Objects.equals(a, b)) {
        // false, no exception
    }
  7. Compare enums with different types. You cannot directly compare enums of different types with == or equals() because they are different classes. The compiler will reject == between incompatible types. If you need to compare by name, use name() or toString().

    enum Color { RED, GREEN }
    enum Size { SMALL, LARGE }
    // Color.RED == Size.SMALL // compile error
  8. Use switch statements for cleaner comparisons. Java allows switching on enums, which internally uses ordinal() and is efficient.

    switch (today) {
        case MONDAY:
            System.out.println("Start of week");
            break;
        case FRIDAY:
            System.out.println("TGIF");
            break;
        default:
            System.out.println("Other day");
    }

Common Mistakes

  • Using equals() on a null reference – This throws NullPointerException. Always check for null or use Objects.equals().
  • Assuming compareTo() compares by name – It compares by ordinal (declaration order), not alphabetically. If you need alphabetical order, compare name() strings.
  • Comparing enums of different types – Attempting == between different enum types causes a compile error. Even equals() will return false because the classes differ.
  • Overriding equals() or hashCode() in an enum – You cannot override these methods in an enum; the compiler forbids it. If you try, you’ll get a compile error. Use fields and methods instead.
  • Using toString() for comparison when you mean name() – toString() can be overridden to return a custom label, while name() always returns the constant name. Comparing with toString() may give unexpected results if overridden.

Tips

  • Prefer == over equals() for enums – It’s faster and avoids a method call, and it’s safe because each constant is a singleton.
  • Use compareTo() when you need to sort or find min/max – For example, Collections.min(list) works on enums because they are Comparable.
  • If you need a custom ordering, add a field to the enum and compare that field manually, or implement a Comparator.
  • In switch statements, use the enum constant name without the enum type prefix (e.g., case MONDAY:, not case Day.MONDAY:).
  • When storing enums in collections, HashSet and HashMap work correctly because hashCode() is also final and consistent with equals().

Troubleshooting

  • Compile error: “incompatible types” when using == – You’re comparing enums of different types. Use name().equals() or a common interface if needed.
  • NullPointerException when calling equals() – The receiver is null. Check for null before calling, or use Objects.equals(a, b).
  • Your enum comparison returns unexpected order – Remember compareTo() uses declaration order. If you need a different order, add an int field and compare that.
  • Switch statement doesn’t compile – Ensure you’re using the enum type in the switch expression and case labels are unqualified constant names. Also, the enum must be in scope.
  • You accidentally overrode toString() and now comparisons fail – If you compare using toString(), it might return a custom string. Use name() instead for the constant’s identifier.

Frequently Asked Questions

  • Is it safe to use == to compare enums? Yes, because each enum constant is a singleton instance. The JVM guarantees only one instance per constant, so == is equivalent to equals() and is the recommended approach.
  • Can I override equals() in an enum? No, the compiler prevents overriding equals() and hashCode() in enums. This is intentional to maintain the singleton guarantee.
  • What’s the difference between name() and toString()? name() returns the exact identifier used in the declaration (e.g., “MONDAY”). toString() by default returns the same, but you can override it to return a friendlier label. For comparisons, use name() to avoid surprises.
  • How do I compare enums alphabetically? Use a.name().compareTo(b.name()) or create a Comparator that uses name().
  • Can I compare an enum to a String? Not directly. You can compare the enum’s name() or toString() to a String using .equals(), but the types are different.
  • Does compareTo() throw an exception if the enum is null? Yes, calling compareTo() on a null reference will throw NullPointerException. Always check for null before calling.

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.