Friday 9 October 2015

Java Enums are Inherently Comparable


enums are automatically Comparable, there is no need to explicitly add the "implements Comparable".

When we check the enum weather it is instance of Comparable, it return true.


enum Day {
       SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
       THURSDAY, FRIDAY, SATURDAY;
}

public class EnumTest {
       public static void main(String[] args) {
              Day day = Day.MONDAY;
              String isComparable = "Not Comparable";

              if(day instanceof Comparable) {
                     isComparable = "Comparable";
              }

              System.out.println(isComparable);
       }
}
Output: Comparable



No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...