Home > Java > javaTutorial > Java Double comparison example

Java Double comparison example

王林
Release: 2023-09-01 18:41:02
forward
1576 people have browsed it

Java Double比较示例

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.

Comparing Double Precision Numbers in Java

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.

Use == operator

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.

Example

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);
   }
}
Copy after login

Output

Value after addition: 0.30000000000000004
false
Value after addition: 0.5
true
Copy after login

illustrate

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.

Use compareTo() method

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.

grammar

firstObject.compareTo(secondObject);
Copy after login

Example

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");
      }
   }
}
Copy after login

Output

d1 is greater than d2
Copy after login

Use compare() method

It is a static method of the 'java.lang.Double' class. It works similarly to the compareTo() method.

Example

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");
      }
   }
}
Copy after login

Output

d1 is less than d2
Copy after login

in conclusion

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template