Double is one of the primitive data types available in Java to represent decimal point numbers. It belongs to the floating point type, also known as real number, and is used when calculations require decimal values. For example, to express the result of 1/5, which is 0.2, the results of sine and cosine also need a decimal point. In addition to the representation of decimal values, we can also perform comparison operations between two double-type variables. This article aims to explore possible ways of comparing two variables with the help of example programs.
In this section, we will explain how to compare Doubles in Java. We can use the following methods -
Use the == operator
Use compareTo() method
Use the compare() method
Let’s discuss them one by one with a sample program.
The "==" operator is one of the relational operators available in Java. It is used to compare values of two data types. More precisely, it performs an equality test meaning it checks if the given values are equal and returns true if the two values are equal, false otherwise.
However, many Java developers do not recommend using the equality operator due to rounding errors. Rounding errors are very common and occur when we work with floating point numbers. Therefore, when we use the "==" operator to compare double values, we may get wrong results.
In this example, we will use the '==' operator to compare two double values.
public class Demo { public static void main(String[] args) { double data1 = 0.30; double data2 = 0.20 + 0.10; System.out.println("Value after addition: " + data2); // checking equality of first operation System.out.println(data1 == data2); double data3 = 0.50; double data4 = 0.10 + 0.10 + 0.10 + 0.10 + 0.10; System.out.println("Value after addition: " + data4); // checking equality of second operation System.out.println(data3 == data4); } }
Value after addition: 0.30000000000000004 false Value after addition: 0.5 true
In the above code, when we perform the first addition operation, the expected value of 'data2' is 0.30, but the output result is wrong. However, when we do another similar addition operation, we get the exact result and the equality test passes. Therefore, it is not recommended to use the '==' operator to compare two double values.
It is a method of the 'java.lang.Double' class that can be used to compare two Double values. It returns 0 when the first object is equal to the passed object, a positive value if the first object is greater than the passed object, and a negative value otherwise.
firstObject.compareTo(secondObject);
In the following example, we will initialize two Double variables and compare them using the compareTo() method.
import java.lang.*; public class Demo { public static void main(String args[]) { // declaring and initializing two double variable Double d1 = Double.valueOf(395.24); Double d2 = Double.valueOf(323.30); // comparing and storing the result to an int variable int res = d1.compareTo(d2); if(res > 0) { System.out.println("d1 is greater than d2"); } else if(res < 0) { System.out.println("d1 is less than d2"); } else { System.out.println("d1 is equal to d2"); } } }
d1 is greater than d2
It is a static method of the 'java.lang.Double' class. It works similarly to the compareTo() method.
The following example uses the compare() method to check whether two Double values are equal.
import java.lang.*; public class Demo { public static void main(String args[]) { // declaring and initializing two double variable Double d1 = Double.valueOf("195.24"); Double d2 = Double.valueOf("323.30"); int res = Double.compare(d1, d2); // comparing and storing the result to an int variable if(res > 0) { System.out.println("d1 is greater than d2"); } else if(res < 0) { System.out.println("d1 is less than d2"); } else { System.out.println("d1 is equal to d2"); } } }
d1 is less than d2
We have discussed three ways to compare Double values with the help of a program. Try to avoid the first approach, which is using the "==" operator to compare double values, as it may lead to wrong results due to rounding errors. Two other built-in methods named compare() and compareTo() will provide us with accurate results.
The above is the detailed content of Java Double comparison example. For more information, please follow other related articles on the PHP Chinese website!