Java - Double compareTo() method



Description

The Java Double compareTo() method compares two Double objects numerically. There are two ways in which comparisons performed by this method differ from those performed by the Java language numerical comparison operators (<, <=, ==, >= >) when applied to primitive double values −

  • Double.NaN is considered by this method to be equal to itself and greater than all other double values (including Double.POSITIVE_INFINITY).
  • 0.0d is considered by this method to be greater than -0.0d.

Declaration

Following is the declaration for java.lang.Double.compareTo() method

public int compareTo(Double anotherDouble)

Parameters

anotherDouble − This is the Double to be compared.

Return Value

This method returns the value 0 if anotherDouble is numerically equal to this Double; a value less than 0 if this Double is numerically less than anotherDouble; and a value greater than 0 if this Double is numerically greater than anotherDouble.

Exception

NA

Example 1

The following example shows the usage of Double compareTo() method to check a value is greater than another value or not. We're having two Double objects and using compareTo() method, we're comparing these double objects and then result is compared to 0. If result is greater than 0 then first number is greater than second number. If result is less than 0 then first number is less than second number. Otherwise both values are same.

package com.tutorialspoint;
public class DoubleDemo {
   public static void main(String[] args) {

      // compareTos the two specified double objects
      Double d1 = new Double("11.50");
      Double d2 = new Double("8.50");
      int retval =  d1.compareTo(d2);
    
      if(retval > 0) {
         System.out.println("d1 is greater than d2");
      } else if(retval < 0) {
        System.out.println("d1 is less than d2");
      } else {
         System.out.println("d1 is equal to d2");
      }
   }
} 

Output

Let us compile and run the above program, this will produce the following result −

d1 is greater than d2

Example 2

The following example shows the usage of Double compareTo() method to check a value is less than another value or not. We're having two Double objects and using compareTo() method, we're comparing these double objects and then result is compared to 0. If result is greater than 0 then first number is greater than second number. If result is less than 0 then first number is less than second number. Otherwise both values are same.

package com.tutorialspoint;
public class DoubleDemo {
   public static void main(String[] args) {

      // compareTos the two specified double values
      Double d1 = new Double("8.50");
      Double d2 = new Double("11.50");
      int retval =  d1.compareTo(d2);
    
      if(retval > 0) {
         System.out.println("d1 is greater than d2");
      } else if(retval < 0) {
        System.out.println("d1 is less than d2");
      } else {
         System.out.println("d1 is equal to d2");
      }
   }
} 

Output

Let us compile and run the above program, this will produce the following result −

d1 is less than d2

Example 3

The following example shows the usage of Double compareTo() method to check a value is same as another value or not. We're having two Double objects and using compareTo() method, we're comparing these double objects and then result is compared to 0. If result is greater than 0 then first number is greater than second number. If result is less than 0 then first number is less than second number. Otherwise both values are same.

package com.tutorialspoint;
public class DoubleDemo {
   public static void main(String[] args) {

      // compareTos the two specified double values
      Double d1 = new Double("11.50");
      Double d2 = new Double("11.50");
      int retval =  d1.compareTo(d2);
    
      if(retval > 0) {
         System.out.println("d1 is greater than d2");
      } else if(retval < 0) {
        System.out.println("d1 is less than d2");
      } else {
         System.out.println("d1 is equal to d2");
      }
   }
} 

Output

Let us compile and run the above program, this will produce the following result −

d1 is equal to d2
java_lang_double.htm
Advertisements