Java Enum.compareTo(E o)

Syntax

Enum.compareTo(E o) has the following syntax.

public final int compareTo(E o)

Example

In the following code shows how to use Enum.compareTo(E o) method.


/*from w  ww .ja  v  a2  s . c om*/
enum Tutorial {  
   HTML, CSS, Java; 
} 
 
public class Main { 

  public static void main(String args[]) {
    Tutorial t1 = Tutorial.HTML; 
    Tutorial t2 = Tutorial.CSS; 
    Tutorial t3 = Tutorial.Java; 
    
    if(t1.compareTo(t2) >  0) {
       System.out.println(t2 + " completed before " + t1); 
    }
    
    if(t1.compareTo(t2) < 0) {
       System.out.println(t1 + " completed before " + t2); 
    }
    
    if(t1.compareTo(t3) == 0) { 
       System.out.println(t1 + " completed with " + t3); 
    }
  } 
}

The code above generates the following result.





















Home »
  Java Tutorial »
    java.lang »




Boolean
Byte
Character
Class
Double
Enum
Float
Integer
Long
Math
Number
Object
Package
Process
ProcessBuilder
Runnable
Runtime
SecurityManager
Short
StackTraceElement
StrictMath
String
StringBuffer
StringBuilder
System
Thread
ThreadGroup
ThreadLocal
Throwable