Javaexercise.com

How To Compare Enum Members Using == Or Equals() Method In Java?

Enum is the short form of enumeration, which means specifically listed or a list of named constants.

Enum was first introduced in Java 5. It contains a fixed set of constants. It can also contain methods. It can be used to store directions (NORTH, SOUTH, EAST, WEST), days(SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY), colors(VIOLET, BLUE, YELLOW, PINK, GREEN, BLACK, WHITE), etc.

According to the Java naming convention, we should have all the constants in capital letters. So, we have written in capital letters.

There are two ways for comparing the enum in Java:

  1. By using == operator

  2. By using equals() method

Let's understand one by one with code examples.

Comparing enum By using == operator in Java

The == operator is a binary operator. It compares the two operands and returns either true or false. It never throws NullPointerException.

some key features are:

  • It's faster.

  • It's safer at run-time.

  • It's safer at compile-time.

In the below example, we are comparing an enum member with a null. So here we are not getting any exception. For better understanding see the example below.

// Java code to demonstrate enum members comparison by using == operator.
class JavaExercise {
    public enum Month {
        JAN,
        FEB,
        MAR,
        APR,
        MAY,
        JUN,
        JUL,
        SEPT,
        OCT,
        NOV,
        DEC
    }
​
    public static void main(String[] args) {
        Month m = null;
​
        // Comparing an enum member with null
        // using == operator
        System.out.println(m == Month.FEB);
    }
}

Output:

false

Let's take one more example. Here, we have used the Days enum to compare its values by using the == operator. The first value will return false and the second will return true. See the example below.

// Java program to demonstrate enum members comparison by using == operator.
enum Days {
    MON,
    TUE,
    WED,
    THU,
    FRI,
    SAT,
    SUN
}
public class JavaExercise {
    public static void main(String[] args) {
        boolean result = isSun(Days.WED);
        System.out.println(result);
        result = isSun(Days.SUN);
        System.out.println(result);
    }
​
    public static boolean isSun(Days day) {
        if (day == Days.SUN) {
            return true;
        }
        return false;
    }
}

Output:

false

true

Comparing two different enum using == operator in Java

In the below code, we have compared an enum member with another enum member. We have used the == operator for comparison.

Here, we are getting compilation failure. So from this, we can conclude that when we use == operator for enum member comparison then it performs type compatibility check at the compile time.

// Java program to demonstrate enum members comparison
class JavaExercise {
    public enum Day {
        MON,
        TUE,
        WED,
        THU,
        FRI,
        SAT,
        SUN
    }
​
    public enum Month {
        JAN,
        FEB,
        MAR,
        APR,
        MAY,
        JUN,
        JULY
    }
​
    public static void main(String[] args) {
        // Comparing two enum members which are from different enum type
        // using == operator
        System.out.println(Month.JAN == Day.MON);
​
    }
}

​Output:

JavaExercise.java:26: error: incomparable types: Month and Day
        System.out.println(Month.JAN == Day.MON);
                                     ^

Comparing enum By using equals() method in Java

In Java, the equals() method compares two values and returns a boolean value, either true or false. We can use this method for comparing enum. The equals() method throws NullPointerException if the enum is null.

In the below example, we are comparing an enum member with a null. So here we are getting NullPointerException. For better understanding see the example below.

// Java program to demonstrate enum members comparison  using equals() method.
class JavaExercise {
    public enum Month {
        JAN,
        FEB,
        MAR,
        APR,
        MAY,
        JUN,
        JUL,
        SEPT,
        OCT,
        NOV,
        DEC
    }
​
    public static void main(String[] args) {
        Month m = null;
​
        // Comparing an enum member with null
        // using .equals() method
        System.out.println(m.equals(Month.FEB));
    }
}

​Output:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "JavaExercise$Month.equals(Object)" because "<local1>" is null
        at JavaExercise.main(JavaExercise.java:22)

let's take another example. Here, we have used the Days enum to compare its values by using the equals() method. The first value will return false and the second will return true.

// Java program to demonstrate enum members comparison  using equals() method.
enum Days {
    MON,
    TUE,
    WED,
    THU,
    FRI,
    SAT,
    SUN
}
​
public class JavaExercise {
    public static void main(String[] args) {
        boolean result = isSun(Days.WED);
        System.out.println(result);
        result = isSun(Days.SUN);
        System.out.println(result);
    }
​
    public static boolean isSun(Days day) {
        if (day.equals(Days.SUN)) {
            return true;
        }
        return false;
    }
}

Output:

false

true

Comparing two different enum using equals() method in Java

In the below code, we have compared an enum member with another enum member. We have used the equals() method for comparison. Here, we are not getting any compilation failure. So from this, we can conclude that the equals() method never worries about the types of both the arguments (it gives false for the incomparable types).

// Java program to demonstrate enum members comparison
class JavaExercise {
    public enum Day {
        MON,
        TUE,
        WED,
        THU,
        FRI,
        SAT,
        SUN
    }
​
    public enum Month {
        JAN,
        FEB,
        MAR,
        APR,
        MAY,
        JUN,
        JULY
    }
​
    public static void main(String[] args) {
​
        // Comparing two enum members which are from different enum type
        // using .equals() method
        System.out.println(Month.JAN.equals(Day.MON));
    }
}

Output:

false