Java - compareTo() Method



Description

The method compares 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 the same type.

Syntax

public int compareTo( NumberSubClass referenceName )

Parameters

Here is the detail of parameters −

  • referenceName − This could be a Byte, Double, Integer, Float, Long, or Short.

Return Value

  • If the Integer is greater than the argument then 1 is returned.
  • If the Integer is equal to the argument then 0 is returned.
  • If the Integer is less than the argument then -1 is returned.

Example 1

In this example, we're showing the usage of compareTo() method to compare Integers. We've created an Integer variable x and initialized to a value of 5. Then using compareTo() methods, we're comparing with multiple values like 3, 5 and 8 to cover multiple cases.

public class Test {
   public static void main(String args[]) {
      Integer x = 5;
      
      // Integer value is greater than the argument (5 > 3) so, output is 1
      System.out.println(x.compareTo(3));
      
      // Integer value is equal to the argument so, output is 0
      System.out.println(x.compareTo(5));
      
      // Integer value is less than the argument (5 < 8) so, output is −1
      System.out.println(x.compareTo(8));            
   }
}

This will produce the following result −

Output

1
0
-1

Example 2

In this example, we're showing the usage of compareTo() method to compare float values. We've created an Float variable x and initialized to a value of 5.0. Then using compareTo() methods, we're comparing with multiple values like 3.0, 5.0 and 8.0 to cover multiple cases.

public class Test {
   public static void main(String args[]) {
      Float x = (float) 5.0;
      
      // Float value is greater than the argument (5.0 > 3.0) so, output is 1
      System.out.println(x.compareTo((float) 3.0));
      
      // Float value is equal to the argument so, output is 0
      System.out.println(x.compareTo((float) 5.0));
      
      // Float value is less than the argument (5.0  < 8.0) so, output is −1
      System.out.println(x.compareTo((float) 8.0));            
   }
}

This will produce the following result −

Output

1
0
-1

Example 3

In this example, we're showing the usage of compareTo() method to compare double values. We've created an Double variable x and initialized to a value of 5.0. Then using compareTo() methods, we're comparing with multiple values like 3.0, 5.0 and 8.0 to cover multiple cases.

public class Test {
   public static void main(String args[]) {
      Double x = 5.0;
      
      // Double value is greater than the argument (5.0>3.0) so, output is 1
      System.out.println(x.compareTo(3.0));
      
      // Double value is equal to the argument so, output is 0
      System.out.println(x.compareTo(5.0));
      
      // Double value is less than the argument (5.0<8.0) so, output is −1
      System.out.println(x.compareTo(8.0));            
   }
}

This will produce the following result −

Output

1
0
-1
java_numbers.htm
Advertisements