Difference Between ordinal() and compareTo() in Java Enum - GeeksforGeeks

02 Feb, 2021
Enumerations serve the purpose of representing a group of named constants in a programming language. If we want to represent a group named constant then we should go for Enum. Every enum constant is static. Hence, we can access it by using enum Name.
Example:
enum Day{
  SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY;
}
The java.lang.Enum.ordinal() tells about the ordinal number(it is the position in its enum declaration, where the initial constant is assigned an ordinal of zero) for the particular enum.
ordinal() method is a non-static method, it means that it is accessible with the class object only and if we try to access the object of another class it will give the error. It is a final method, cannot be overridden.
The ordinal() method returns the ordinal of the enum constant. (its position in its enum declaration, where the first constant is assigned an ordinal of zero).
Syntax: 
public final int ordinal();
Return Value:
This method returns the ordinal of this enumeration constant.
Example:
Day d1 = Day.TUESDAY;
System.out.println("the ordinal value is :" + d1.ordinal());

Output:
the ordinal value is : 2
Java
// Java program to show the usage of // ordinal() method of java enumeration   enum Day{    SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY;}  class Main{          public static void main(String args[])    {       Day days[] = Day.values();       for(Day d : days)       System.out.print(d.ordinal() + " " );    }}
0 1 2 3 4 5 6 
The compareTo() method of Enum class compares this enum object with the defined object for order. Enum constants can only be compared to other enum constants of the same type.
Returns:
A negative integer, if this enum is less than the defined object.zero, if this enum is equal to the defined object.a positive integer, if this enum is greater than the defined object.
A negative integer, if this enum is less than the defined object.
zero, if this enum is equal to the defined object.
a positive integer, if this enum is greater than the defined object.
Syntax:
int compareTo(Object obj)
Example:
Day day1 = Day.SUNDAY;
Day day2 = Day.MONDAY;
System.out.println(day1.compareTo(day2));
Since the ordinal value of day1 is less than day2 so it will return a negative value.
Java
// Java program to show the usage of // compareTo() method of java enumeration   enum Day{    SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY;}  class GFG{          public static void main(String args[])    {        Day d1 = Day.SUNDAY;          Day d2 = Day.MONDAY;          Day d3 = Day.TUESDAY;                // will return negative value since d1 < d2            System.out.println(d1.compareTo(d2) );                // will return positive value since d2 > d1          System.out.println(d2.compareTo(d1) );                // will return 0 since d3 == d3        System.out.println(d3.compareTo(d3) );             }}
-1
1
0
Ordinal() Method
CompareTo() Method
Returns A negative integer, if this enum is less than the defined object.
Returns zero, if this enum is equal to the defined object.
Returns a positive integer, if this enum is greater than the defined object.
Example: 
enum Day{
SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY;
}
Day d = Day.SUNDAY;
d.ordinal() // 0
  
Example:
enum Day{
SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY;
}
Day d1 = Day.SUNDAY;
Day d2 = Day.MONDAY;
d1.compareTo(d2) // since d1 < d2 output will be -ve
Java-Enumeration
Picked
Difference Between
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Difference between var, let and const keywords in JavaScript
Difference Between Method Overloading and Method Overriding in Java
Difference between Internal and External fragmentation
Difference between Prim's and Kruskal's algorithm for MST
Differences and Applications of List, Tuple, Set and Dictionary in Python
Arrays in Java
Split() String method in Java with examples
For-each loop in Java
Reverse a string in Java
HashMap in Java with Examples