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.
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:
Comparing 4.0 and 4.0 : 0
Comparing 5.6 and 4.2 : 1
Comparing 3.6 and 7.4 : -1