Easy Tutorial
❮ Java String Intern Java Linkedlist ❯

Java compareTo() Method

Java Number Class


The compareTo() method is used to compare a Number object with the method's argument. It can be used for comparing Byte, Long, Integer, etc.

This method is used for comparing two same data types; two different types of data cannot be compared using this method.

Syntax

public int compareTo( NumberSubClass referenceName )

Parameters

referenceName -- Can be a Byte, Double, Integer, Float, Long, or Short type parameter.

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));            
     }
}

Compiling the above program will produce the following output:

1
0
-1

Java Number Class

❮ Java String Intern Java Linkedlist ❯