Java compareTo () method
compareTo () method for parameter Number of objects and methods are compared. It can be used to compare Byte, Long, Integer and so on.
The method used to compare two of the same data type, two different types of data can not be compared in this way.
grammar
public int compareTo( NumberSubClass referenceName )
parameter
referenceName - can be a Byte, Double, Integer, Float, Long or Short types of arguments.
return value
If the parameter is equal to the specified number of returns 0.
If the number is less than the specified parameters return -1.
If the number is greater than the specified parameter is returned.
Examples
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)); } }
Compile the above program, the output is:
1 0 -1