How do we compare the members of enums in Java?


Enumeration (enum) in Java is a datatype which stores a set of constant values. You can use enumerations to store fixed values such as days in a week, months in a year etc.

enum Days {
   SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
}

You can also define an enumeration with custom values to the constants declared. But you need to have an instance variable, a constructor and getter method to return values.

You can compare the members of two enums using the “==” operator or the equals() method.

Comparing enum members using “==”

  • If you compare two enum members with the “==” operator, both must be of same type else a compile time error is generated.
enum Vehicles {
   ACTIVA125, ACTIVA5G, ACCESS125, VESPA, TVSJUPITER;
}
enum Scoters {
   ACTIVA125, VESPA, TVSJUPITER, ACTIVA5G, ACCESS125,;
}
public class EnumComparison {
   public static void main(String args[]) {
      Vehicles veh[] = Vehicles.values();
      Scoters sco[] = Scoters.values();
      //comparing enum members of two different types
      System.out.println(veh[1]==sco[1]);
   }
}

Compile time error

EnumComparison.java:14: error: incomparable types: Vehicles and Scoters
System.out.println(veh[1]==sco[1]);
^
1 error
  • Using the “==” operator you can compare the members enum that are of same type (only).

Example

Let us create an enum with name Vehicles with 5 constants representing models of 5 different scoters with their prices as values, as shown below −

enum Vehicles {
   //Constants with values
   ACTIVA125(80000), ACTIVA5G(70000), ACCESS125(75000), VESPA(90000), TVSJUPITER(75000);
   //Instance variable
   private int price;
   //Constructor to initialize the instance variable
   Vehicles(int price) {
  this.price = price;
   }
   //Static method to display the price
   public int getPrice(){
      return this.price;
   }
}

In the Following Java program, the displayPrice() method accepts a constant of the enum Vehicles and, compares it with all the other constants in Vehicles using the “==” operator and, displaces the value of the particular constant (price) incase of a match.

public class EnumerationExample {
   public void displayPrice(Vehicles enum) {
      if (enum == Vehicles.ACTIVA125) {
         System.out.println("The price of "+enum+" is:"+enum.getPrice());
      } else if (enum == Vehicles.ACTIVA5G) {
         System.out.println("The price of "+enum+" is:"+enum.getPrice());
      } else if (enum == Vehicles.ACCESS125) {
         System.out.println("The price of "+enum+" is:"+enum.getPrice());
      } else if (enum == Vehicles.VESPA) {
         System.out.println("The price of "+enum+" is:"+enum.getPrice());
      } else if (enum == Vehicles.TVSJUPITER) {
         System.out.println("The price of "+enum+" is:"+enum.getPrice());
      } else {
         System.out.println("Model not found");
      }
   }
   public static void main(String args[]) {
      EnumerationExample obj = new EnumerationExample();
      obj.displayPrice(Vehicles.ACTIVA125);
      obj.displayPrice(Vehicles.ACTIVA5G);
      obj.displayPrice(Vehicles.ACCESS125);
      obj.displayPrice(Vehicles.VESPA);
      obj.displayPrice(Vehicles.TVSJUPITER);
   }
}

Output

The price of Activa125 is:80000
The price of ACTIVA5G is:70000
The price of ACCESS125 is:75000
The price of VESPA is:90000
The price of TVSJUPITER is:75000

Comparing enum members using “==”

  • You can compare two enum members of same or different type using the equals() method.
enum Vehicles {
   ACTIVA125, ACTIVA5G, ACCESS125, VESPA, TVSJUPITER;
}

enum Scoters {
   ACTIVA125, VESPA, TVSJUPITER, ACTIVA5G, ACCESS125,;
}
public class EnumerationComparison {
   public static void main(String args[]) {
      Vehicles veh[] = Vehicles.values();
      Scoters sco[] = Scoters.values();
      //comparing enum members of two different types
      System.out.println(veh[1].equals(sco[1]));
      System.out.println(veh[1].equals(veh[1]));
   }
}

Output

False
true

Updated on: 30-Jun-2020

528 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements