In Java, there can be situations where the programmer needs to compare complex sort of values that can’t be analyzed easily. For instance, analyzing the identical values having differences in their decimal point values. In such situations, the “Double.compare()” method in Java is assistive in determining the equivalent, greater, or smaller values by returning the allocated returned values in each case.
This article will elaborate on using and implementing the “Double.compare()” method in Java.
What is the “Double.compare()” Method in Java?
The “compare()” is a static method of the “Double” class that is utilized to compare the two double values.
Syntax
In the above syntax, “double a” and “double b” correspond to the double values that need to be compared with each other based on the following parameters of “compare()” method:
Returned Value | Comparison Outcome |
0 | If both the values are the same. |
-1 | If the former value, i.e., “a” is less than the latter value, i.e., “b”. |
1 | If the former value is greater than the latter value. |
Example 1: Applying the “Double.compare()” Method in Java to Compare the Specified Double Values
In this example, the “Double.compare()” method can be applied to compare the specified two double values:
public static void main(String args[]){
double value1 = 150d;
double value2 = 250d;
System.out.println("The comparison becomes: "+ Double.compare(value1, value2));
System.out.println("The comparison becomes: "+ Double.compare(value2, value1));
System.out.println("The comparison becomes: "+ Double.compare(value1, value1));
}}
According to the above code snippet, apply the following steps:
- Initialize the two provided double values.
- After that, apply the “compare()” method accumulating the initialized values differently as its argument.
- Here, all the possible outcomes, i.e., “return values” are covered at each step.
- Lastly, display the returned values based on the applied comparison.
Output
In this output, it can be observed that the corresponding outcome is generated based on the applied comparison differently.
Before proceeding to the next example, make sure to include the below-provided package to enable user input:
Example 2: Applying the “Double.compare()” Method in Java to Compare the User Input Double Values
This example compares the user input double values by placing the method’s return value as an exception in the “if/else” statement:
public static void main(String args[]){
Scanner object = new Scanner(System.in);
System.out.println("Enter the first double value: ");
double x = object.nextDouble();
System.out.println("Enter the second double value: ");
double y = object.nextDouble();
if (Double.compare(x, y) == 0) {
System.out.println("The double values are equal.");
}
else if (Double.compare(x, y) == 1) {
System.out.println("The first double is greater than second.");
}
else if (Double.compare(x, y) == -1) {
System.out.println("The second double is greater than first.");
object.close();
}
}}
According to this code snippet, apply the following steps:
- First of all, create a “Scanner” object using the “new” keyword and the “Scanner()” constructor, respectively.
- The “in” parameter reads the input and the “nextDouble()” method takes the user input as double.
- Now, apply the “compare()” method considering each of the returned values, i.e., “0”, “1”, and “-1”, respectively using the “if/else” statement.
- Lastly, log the corresponding message with respect to the invoked condition.
Output
In this output, it can be seen that each of the conditions is invoked based on the compared user input double values.
Example 3: Applying the “Double.compare()” Method in Java to Compare the Double Objects
In this example, the discussed method can be implemented to compare the double objects:
public static void main(String args[]){
double value1 = new Double(150d);
double value2 = new Double(250d);
System.out.println("The comparison becomes: "+ Double.compare(value1, value2));
System.out.println("The comparison becomes: "+ Double.compare(value2, value1));
System.out.println("The comparison becomes: "+ Double.compare(value1, value1));
}}
In this example, create two “double” objects via the “new” keyword and the “Double()” constructor, respectively comprising the stated double values. After that, likewise, compare the created objects with the help of the “Double.compare()” method and log the corresponding outcome.
Output
In this outcome, it can be implied that the double values are compared accordingly.
Conclusion
The “compare()” is a static method of the “Double” class in Java which is utilized to compare the two double values and returns the values instead based on the applied comparison. This method can be utilized to compare the specified, user input double values, or the double objects. This blog is guided to utilize the “Double.compare()” method in Java.