Copyright © tutorialspoint.com

Java - compareTo() Method

previous next


Description:

The method comapres the Number object that invoked the method to the argument. It is possible to compare Byte,Long,Integer etc.

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

Syntax:

public int compareTo( NumberSubClass referenceName )

Parameters:

Here is the detail of parameters:

Return Value :

Example:

public class Test{ 
   public static void main(String args[]){
      Integer x = 5;
      System.out.println(x.compareTo(3));
      System.out.println(x.compareTo(5));
      System.out.println(x.compareTo(8));            
     }
}

This produces following result:

1
0
-1

previous next

Copyright © tutorialspoint.com