Signup/Sign In
LAST UPDATED: AUGUST 26, 2020

Java Double compareTo() Method

Java compareTo() method belongs to the Double class. This method is similar to the compare() method of the same class but the only difference is that here the Double Objects are compared numerically rather than the double values.

It must be noted that Double.NaN value is considered to be equal to itself and greater than any other number and 0.0d is considered to be greater than -0.0d.

The object1.compareTo(float object2) method returns the value

  • 0; if object1 is equal to object2
  • -1; if object1 is less than object2
  • 1; if object1 is greater than object2

Syntax:

public int compareTo?(Double anotherDouble)

Parameter:

The parameter includes the Double object to be compared.

Returns:

0,-1,1 depending upon the value of the argument double value.

Example 1:

Here, four objects n1, n2, n3, n4 are taken with respective double values and are checked for the values are either less, more, or equal than the compared values.

import java.lang.Double;

public class StudyTonight 
{  
    public static void main(String[] args) 
    {          
      Double ob1 = 100.0;  
      Double ob2 = 200.0;  
      Double ob3 = 300.0;  
      Double ob4 = 100.0;  
          
        System.out.println(ob1.compareTo(ob2));  //Output will be less than zero
        
        System.out.println(ob3.compareTo(ob1)); // Output will be greater than zero  
        
        System.out.println(ob1.compareTo(ob4));  // Output will be equal to zero
    }  
}  


-1
1
0

Example 2:

Here, an array of Double object values is created and every object in the array is compared with the first object of the array.

import java.lang.Double;

public class StudyTonight 
{  
    public static void main(String[] args) 
     {          
      Double [] ob = {-100.0,-200.0,300.0,-100.0}; 
          for(int i=0;i<4;i++)
            {
               System.out.println(ob[0].compareTo(ob[i]));  // comparing each object of array with first object
            }
      }  
}  


0
1
-1
0

Example 3:

Here is a general example where the user can enter the numbers of his choice and get the desired output. The try and catch block is used for handling any exception taking place during the execution of the program. (For example, the user enters a String, etc. in place of the double value).

import java.util.Scanner; 
import java.lang.Double;
public class StudyTonight 
{  
    public static void main(String[] args) 
    {      
        Scanner sc = new Scanner(System.in);  
        System.out.print("Enter first and second number ");  
        try
          {
           Double n1 = sc.nextDouble();  
           Double n2 = sc.nextDouble();  
           int r =  n1.compareTo(n2);    
             if(r > 0)
             {  
               System.out.println("first number is greater");  
             }
             else if(r< 0) 
             {  
               System.out.println("second number is greater");  
             } 
             else
             {  
               System.out.println("both numbers are equal");
             }
           }
          catch(Exception e)
            {
              System.out.println("Error!!");
            }
          
     }  
}  


Enter first and second number 78.09 -56.87
first number is greater
**************************************************
Enter first and second number -89.09 -44.8
second number is greater
**************************************************
Enter first and second number ox667 0x556
Error!!

Live Example:

Here, you can test the live code example. You can execute the example for different values, even can edit and write your examples to test the Java code.



About the author:
A Computer Science and Engineering Graduate(2016-2020) from JSSATE Noida. JAVA is Love. Sincerely Followed Sachin Tendulkar as a child, M S Dhoni as a teenager, and Virat Kohli as an adult.