Java.lang Package Classes

Java Enum - equals() Method



The java.lang.Enum.equals() method returns true if the specified object is equal to this enum constant.

Syntax

public final boolean equals(Object other)

Parameters

other Specify the object to be compared for equality with this object.

Return Value

Returns true if the specified object is equal to this enum constant.

Exception

NA.

Example:

In the example below, the java.lang.Enum.equals() method returns true if the given object is equal to the given enum constant.

import java.lang.*;

public class MyClass {
  
  //creating an enum
  public enum weekday{
    MON, TUE, WED, THU, FRI
  }

  public static void main(String[] args) {
    
    weekday d1, d2, d3;

    d1 = weekday.MON;
    d2 = weekday.MON;
    d3 = weekday.TUE;

    if(d1.equals(d2)) {
      System.out.println("d1 and d2 are equal.");
    } else {
      System.out.println("d1 and d2 are different.");
    } 

    if(d1.equals(d3)) {
      System.out.println("d1 and d3 are equal.");
    } else {
      System.out.println("d1 and d3 are different.");
    } 
  }
}

The output of the above code will be:

d1 and d2 are equal.
d1 and d3 are different.

❮ Java.lang - Enum

5