Java Guava | Doubles.compare() method with Examples Last Updated : 11 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report 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: Java // 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); } } Output: Comparing 4.0 and 4.0 : 0 Example 2: Java // 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); } } Output: Comparing 5.6 and 4.2 : 1 Example 3: Java // 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); } } Output: Comparing 3.6 and 7.4 : -1 Comment More infoAdvertise with us Next Article Double.compareTo() Method in Java with Examples S Sahil_Bansall Follow Improve Article Tags : Java Java-Functions java-guava Guava-Booleans Practice Tags : Java Similar Reads Double compare() Method in Java with Examples The compare() method of Double Class is a built-in method in Java that compares the two specified double values. The sign of the integer value returned is the same as that of the integer that would be returned by the function call. Syntax: public static int compare(double d1, double d2) Parameters: 2 min read Double.compareTo() Method in Java with Examples The Double.compareTo() method is a built-in method in Java of java.lang package. This method is used to compare 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, an 3 min read Java Guava | Longs.compare() method with Examples Longs.compare() method of Guava's Longs Class is used to compare the two specified long 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 compa 2 min read Java Guava | Chars.compare() method with Examples Chars.compare() method of Guava's Chars Class is used to compare the two specified char 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 compa 2 min read Java Guava | Floats.compare() method with Examples Floats.compare() method of Floats Class is used to compare the two specified float 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(fl 2 min read DoubleBuffer compareTo() method in Java With Examples The compareTo() method of java.nio.DoubleBuffer class is used to compare one buffer to another. Two double buffers are compared by comparing their sequences of remaining elements lexicographically, without regard to the starting position of each sequence within its corresponding buffer. Pairs of flo 3 min read Like