WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

Java - compareTo() Method

two different types cannot be compared, both the argument and the Number object invoking the method should be of the same type.


Syntex

public int compareTo( NumberSubClass referenceName )

Parameters

what is mean of referenceName ?
Byte, Double, Integer, Float, Long, or Short.

Example

public class Test { 

   public static void main(String args[]) {
      Integer x =5;
      
      System.out.println(x.compareTo(4));
      System.out.println(x.compareTo(5));
      System.out.println(x.compareTo(6)); 
System.out.println(x.compareTo(8));            

   }
}

Output
1
0
-1
-1

Run example »