Double.compareTo() Method in Java with Examples

The java.lang.Double.compareTo() is a built-in method in java that compares two Double objects numerically. This method returns 0 if this object is equal to the argument object, it returns less than 0 if this object is numerically less than the argument object and a value greater than 0 if this object is numerically greater than the argument object.

Syntax:

public int compareTo(Object obj)

Parameter: The method accepts one parameter.
obj – The object which is to be compared to.



Return Values: The function can return three values:

  • equal to 0: Object is equal to the argument object
  • less than 0: Object is less than the argument object
  • greater than 0: Object is greater than the argument object

Below programs illustrates the working of java.lang.Double.compareTo() method:

Program 1: When both objects are different.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// of java.lang.Double.compareTo() method
import java.lang.Math;
  
class Gfg1 {
  
    // Driver code
    public static void main(String args[])
    {
  
        // When two objects are different
        Double obj1 = new Double(124);
        Double obj2 = new Double(167);
        int compareValue = obj1.compareTo(obj2);
  
        if (compareValue == 0)
            System.out.println("object1 and object2 are equal");
        else if (compareValue < 0)
            System.out.println("object1 is less than object2");
        else
            System.out.println("object1 is greater than object2");
    }
}

chevron_right


Output:

object1 is less than object2

Program 2: When no argument is passed.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// of java.lang.Double.compareTo() method
import java.lang.Math;
  
class Gfg1 {
  
    // Driver code
    public static void main(String args[])
    {
        // When no argument is passed
        Double obj1 = new Double(124);
        Double obj2 = new Double(167);
  
        int compareValue = obj1.compareTo();
  
        if (compareValue == 0)
            System.out.println("object1 and object2 are equal");
        else if (compareValue < 0)
            System.out.println("object1 is less than object2");
        else
            System.out.println("object1 is greater than object2");
    }
}

chevron_right


Output:

prog.java:14: error: method compareTo in class Double 
cannot be applied to given types;
        int compareValue = obj1.compareTo();
                               ^
  required: Double
  found: no arguments
  reason: actual and formal argument lists differ in length
1 error

Program 3: When anything other than object is passed as an argument.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// of java.lang.Double.compareTo() method
import java.lang.Math;
  
class Gfg1 {
  
    // Driver code
    public static void main(String args[])
    {
  
        // When anything other than object
        // argument is passed
        Double obj1 = new Double(124);
  
        int compareValue = obj1.compareTo("gfg");
  
        if (compareValue == 0)
            System.out.println("object1 and object2 are equal");
        else if (compareValue < 0)
            System.out.println("object1 is less than object2");
        else
            System.out.println("object1 is greater than object2");
    }
}

chevron_right


Output:

prog.java:15: error: incompatible types: String cannot be converted to Double
        int compareValue = obj1.compareTo("gfg");
                                          ^
Note: Some messages have been simplified; recompile with 
-Xdiags:verbose to get full output
1 error


My Personal Notes arrow_drop_up

Contented with little yet wishing for more

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.