Java Data Type Tutorial - 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   ww w.j ava  2  s  .  c o  m*/
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.