In Java, the “enum” or “enumeration” acts as a group to store a fixed or variable set of constants or methods. They are used when the user knows all possible options at the compile time, like the direction, weekdays, and so on. The “enum” is a special class that offers a functionality similar to Java classes. Users can use its constructors, add instance variables, create methods, or implement interfaces. Other than these functionalities, you can compare the members of enums with each other to achieve type safety(ensures the insertion of only valid values).

This guide illustrates the approaches to compare enum members in Java.

How to Compare Enum Members in Java?

There are two methods to compare the enum members in Java: the “==” operator and the “equals()” method. These methods allow you to check the “Reference Equality” and “Value Equality” of the selected enum members, respectively. 

Method 1: Compare Enum Members Using the “==” Operator

The “==” operator performs reference equality and returns the result in boolean format. This operator returns “true” only if both compared enum members are equal and belong to the same Object or enum. This operator consumes less time and is considered a better approach for enum comparison. 

Syntax

In the below syntax, the “operand1” and “operand2” are the enum members that are being compared:

operand1 == operand2

Take a look at the basic example to compare enum members:

class Compare {
public enum Seasons {
  SPRING,
  SUMMER,
  FALL,
  WINTER,
  SpRInG
}
public static void main(String[] args) {
  Seasons s1,s2, s3, s4;
  s1 = Seasons.WINTER;
  s2 = Seasons.WINTER;
  s3 = Seasons.SPRING;
  s4 = Seasons.SpRInG;

  if(s1 == s2){
  System.out.println(s1 + " and " + s2 + " <- Enums are Equal");
  } else {
  System.out.println(s1 + " and " + s2 + " <- Enums are Not Equal");
  }

  if (s3 == s4) {
  System.out.println(s3 + " and " + s4 + " <- Enums are Equal");
  }
  else{
  System.out.println(s3 + " and " + s4 + " <- Enums are Not Equal Because of Case Sensitivity");
  }
}
}

The above code block:

  • First, create a public enum named “Seasons” that contains fixed values.
  • Next, create four instances of the “Seasons” enum named “s1”, “s2”, “s3”, and “s4”. Moreover, assign enum members to the created instances to perform the comparison.
  • Then, utilize the “if” statement containing the “==” operator to compare the enum members stored in “s1” and “s2” instances. Both “s1” and “s2” enum members contain the same values that’s why the success message gets displayed over the console.
  • After that, compare the “s3” and “s4” instances using the “==” operator and display the message via the “If/else” statement. The “s3” and “s4” instances store the same enum members but in different cases, which makes the comparison “false”.

The generated output shows the result of the comparison between enum members:

Method 2: Compare Enum Members Using the “equals()” Method

The “equals()” method performs the comparison of enum members according to their “values”. It does not check the references or the parent classes from where they originated. It should be used where the user wants to compare enum members according to their current store values only.

Syntax

In the below syntax, the “operand1” and “operand2” are the targeted enum members for comparison:

operand1.equals(operand2)

Let’s take an example to implement and explore more about the “equals()” method.

Example: Compare Enum Members Using the “equals()” Method

In this example, on a random basis two enum members are being compared using the “equals()” method. Then, to see the working of this method, compare two same members but with different cases(SPRING and SpRInG):

class Compare {
public enum Seasons {
  SPRING,
  SUMMER,
  FALL,
  WINTER,
  SpRInG
}
public static void main(String[] args) {
  Seasons s1,s2, s3, s4;
  s1 = Seasons.WINTER;
  s2 = Seasons.WINTER;
  s3 = Seasons.SPRING;
  s4 = Seasons.SpRInG;
  if(s1.equals(s2)){
  System.out.println(s1 + " and " + s2 + " <- Enums are Equal");
  } else {
  System.out.println(s1 + " and " + s2 + " <- Enums are Not Equal");
  }
  if (s3.equals(s4)) {
  System.out.println(s3 + " and " + s4 + " <- Enums are Equal");
  }
  else{
  System.out.println(s3 + " and " + s4 + " <- Enums are Not Equal Because of Case Sensitivity");
  }
}
}

The description of the above code is as follows:

  • First, create an “enum” named “Seasons” and create its four objects inside the “main()” method. These objects represent specific enum members.
  • Then, pass two objects storing enum members into the “equals()” method for comparison. Also, use the “if” statement to display a specific message according to the result provided by the “equals()” method.

The output for the mentioned code confirms the comparison result for enum members:

Which is Faster the “==” Operator Or equals() Method For Enum Comparison?

The execution time for the “==” operator is less than the “equals()” method. Moreover, the “==” operator offers better null safety and throws exceptions for easy identification of code bugs. In addition, its nature of checking the references of both operands for comparison makes it a perfect option for enum members comparison.

Take a look at the below code to retrieve the execution time for both approaches while comparing enum members:

class Compare {
  public enum Seasons {
  SPRING,
  SUMMER,
  FALL,
  WINTER,
  SpRInG
  }

public static void main(String[] args) {
  Seasons s1,s2;
  s1 = Seasons.WINTER;
  s2 = Seasons.WINTER;
  long startTime, endTime;

  //For "==" :
  startTime = System.nanoTime();
  if(s1 == s2){
  System.out.println(s1 + " and " + s2 + " <- Enums are Equal");
  }
  endTime = System.nanoTime();
  System.out.println( endTime - startTime + " <- Execution Time Using == perator\n");
  //For equals():
  startTime = System.nanoTime();
  if(s1.equals(s2)){
  System.out.println(s1 + " and " + s2 + " <- Enums are Equal");
  }
  endTime = System.nanoTime();
  System.out.println( endTime - startTime + " <- Execution Time Using equals() Method");
}
}

In the above code snippet:

  • First, use the same code described in the above sections to compare enum members using the “==” and “equals()” approaches. 
  • Also, apply the “System.nanoTime()” method before and after the mentioned approaches. This method records the time in nanoseconds before and after the completion of comparing tasks. 
  • In the end, subtract the “startTime” from the “endTime” to get the execution time and display it on the console.

The output shows that the “==” operator completed the comparing task in less time frame:

Comparing Two Different Enums Using the “==” Operator and “equals()” Method

The “==” operator and “equals()” method does not compare members of different enums and they generate different results. The “==” operator throws an “Unresolved compilation problem” and the “equals()” method returns a boolean value of “false”. In addition, the “equals()” method does not return anything when used with other statements like “if”:

class Compare {
public enum Semesters {
  SPRING,
  SUMMER,
  FALL,
}

public enum Seasons {
  SPRING,
  WINTER,
  SUMMER,
  FALL,
}

public static void main(String[] args) {
  Seasons sea1 = Seasons.FALL;
  Semesters sem2 = Semesters.FALL;
 
  //For == Operator:
  if(sea1 == sem2){
  System.out.println(sea1 + " and " + sem2 + " <- Enums are Equal");
  }
 
  //With Conditional Statement:
  if(sea1.equals(sem2)){
    System.out.println(sea1 + " and " + sem2 + " <- Enums are Equal");
  }
  //Without Conditional Statement:
  System.out.println(sea1.equals(sem2));
}
}

The explanation of the above code is written below:

  • First, define two enums named “Semesters” and “Seasons”. Then, store their random single member in corresponding enum objects. In our case, the enum member selected from both enums is “FALL”.
  • Next, compare both members from different enums using the “==” operator. Also, utilize the “for” loop to display corresponding messages on the console. The result of this operation generates an exception of “Unresolved compilation problem”.
  • Then, compare the same enum members using the “equals()” method along with the “if” statement. This operation generates no results and returns an empty console.
  • Again compare the enum members but this time only use the “equals()” method and remove the “if” statement part. This operation prints the boolean value of “false” as a result.

The below gif shows the result for each discussed scenario:

That’s all about comparing enum members in Java.

Conclusion

In Java, to compare enum members use the “==” operator or “equals()” method. The “==” operator compares the enum members according to their reference as it compares the location from where they originated. The “equals()” method only compares the member’s values without considering the reference. It also returns the result in boolean format. The choice between usage of both methods is according to your preferences and project requirements. However, the “==” operator is usually considered a better approach due to its reference-checking feature.