Java Guava | Doubles.compare() method with Examples

Doubles.compare() method of Guava’s Doubles Class is used to compare the two specified double values. These values are passed as the parameter and the result of comparison is found as the difference of 1st value and the 2nd value. Hence it can be positive, zero or negative.

Syntax:

public static int compare(double a, double b)

Parameters: This method accepts two parameters:

  • a: which is the first double object to be compared.
  • b: which is the second double object to be compared.

Return Value: This method returns an int value. It returns:

  • 0 if ‘a’ is equal to ‘b’,
  • a positive value if ‘a’ is greater than ‘b’,
  • a negative value if ‘a’ is lesser than ‘b’

Exceptions: The method does not throw any exception.

Below programs illustrate the Double.compare() method:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to show implementation of
// Guava's Doubles.compare() method
  
import com.google.common.primitives.Doubles;
  
class GFG {
    public static void main(String[] args)
    {
        double a = 4.0;
        double b = 4.0;
  
        // compare method in Double class
        int output = Doubles.compare(a, b);
  
        // printing the output
        System.out.println("Comparing " + a
                           + " and " + b + " : "
                           + output);
    }
}

chevron_right


Output:

Comparing 4.0 and 4.0 : 0

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to show implementation of
// Guava's Doubles.compare() method
  
import com.google.common.primitives.Doubles;
  
class GFG {
    public static void main(String[] args)
    {
        double a = 5.6;
        double b = 4.2;
  
        // compare method in Double class
        int output = Doubles.compare(a, b);
  
        // printing the output
        System.out.println("Comparing " + a
                           + " and " + b + " : "
                           + output);
    }
}

chevron_right


Output:

Comparing 5.6 and 4.2 : 1

Example 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to show implementation of
// Guava's Doubles.compare() method
  
import com.google.common.primitives.Doubles;
  
class GFG {
    public static void main(String[] args)
    {
        double a = 3.6;
        double b = 7.4;
  
        // compare method in Double class
        int output = Doubles.compare(a, b);
  
        // printing the output
        System.out.println("Comparing " + a
                           + " and " + b + " : "
                           + output);
    }
}

chevron_right


Output:

Comparing 3.6 and 7.4 : -1


My Personal Notes arrow_drop_up

In love with a semicolon because sometimes i miss it so badly)

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.