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.
public static int compare(double d1, double d2)
Parameters: The function accepts two parameters:
- d1: The first double value to be compared.
- d2: The second double value to be compared.
Return Value: The function returns value as below:
- 0: if d1 is numerically equal to d2.
- Negative value: if d1 is numerically less than d2.
- Positive value: if d1 is numerically greater than d2.
Below programs illustrates the use of Double.compare() function:
Program 1: When two integers are same
Program 2 : When d1<d2
Program 3 : When d1>d2
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.