Compare two enum constants using the == relational operator in Java
Description
The following code shows how to compare two enum constants using the == relational operator.
Example
/*from w w w . jav a 2 s . c o m*/
enum Week {
Monday, Tuesday, Wednesday, Thursday, Friday, Saturaday, Sunday
}
public class Main {
public static void main(String args[]) {
Week aWeekDay;
aWeekDay = Week.Monday;
// Output an enum value.
System.out.println("Value of aWeekDay: " + aWeekDay);
System.out.println();
aWeekDay = Week.Friday;
// Compare two enum values.
if (aWeekDay == Week.Friday)
System.out.println(" Friday.\n");
}
}
The code above generates the following result.
Home »
Java Tutorial »
Java Data Type »
Java Tutorial »
Java Data Type »