Java

How to Compare Doubles in Java?

The Java “double” data type is a basic data type stored as a parameter in the “Double” wrapper class. A double type variable can store 64-bit floating-point numbers. Because Java supports autoboxing, the primitive data type doubles, and the object can be used interchangeably. Also, the Double wrapper class contains many methods utilized to perform operations, such as comparing two double type variables.

This blog will discuss the methods for comparing doubles in Java.

How to Compare doubles in Java?

To compare doubles in Java, you can use:

  • compare() method
  • compareTo() method

We will discuss each of the methods mentioned above one by one!

Method 1: Compare doubles in Java Using compare() Method

The “compare()” method is used to compare two specific double values. It is the static method of the Double Java wrapper class.

Syntax
The syntax of compare() method is:

compare(double d1, double d2)

Here, the compare() method takes two double type values “d1” and “d2” as arguments and returns integer type values: 1,-1, or 0 depending on conditions:

  • It returns 1 if the first value is greater than the second.
  • It will return -1 if the first double value is less than the second.
  • Zero indicates that both double values are equal.

Have a look at the given examples for a better understanding.

Example

In this example, we will compare two double-type values, “db1” and “db2”, with “167.643” and “986.675”, where “d” indicates that it is a double type value:

Double db1= 167.643d;
Double db2= 986.675d;

We will compare the created doubles and store the returned value in “comp”:

double comp = (Double.compare(db1, db2));

Add the if-else-if conditions to check whether the compared values greater than, less than or equals to each other:

if (comp>0) {
    System.out.println("Value of db1 is greater than value of db2");
   }
else if (comp<0){
     System.out.println("Value of db1 is less than value of db2");
   }
else {
    System.out.println("Both values are same");
   }

The given output indicates that the value of “db1” is less than “db2”:

Now, head towards the next method.

Method 2: Compare doubles in Java Using compareTo() Method

The “compareTo()” method is also used to compare doubles in Java, and it also belongs to the “Double” Java wrapper class. In this method, the first double value is compared to the second double value.

Syntax

The syntax of compareTo() method is:

d1.compareTo(double d2)

Here, “d1” is compared to “d2” with the help of the “compareTo()” method.

Example
The values of two double type objects “db1” and “db2” of the Double class will be compared:

Double db1= 986.675d;
Double db2= 986.675d;

Here, we will pass “db2” as an argument to “db1”:

double comp = db1.compareTo(db2);

Specify the if-else-if conditions to check whether the compared values are greater than, less than, or equal to each other:

 if (comp>0) {
     System.out.println("Value of db1 is greater than value of db2");
    }
  else if (comp<0){
        System.out.println("Value of db1 is less than value of db2");
    }
  else {
    System.out.println("Both values are same");
    }

Execution of the above-given program will let you know that both values are the same:

We gathered all the essential instructions related to comparing doubles in Java.

Conclusion

To compare doubles in Java, we have two built-in methods of the Double Java wrapper class: the compare() method and the compareTo() method. Both return integer type values after comparing, where zero shows the values are equal while one indicates the first value is greater than the second value and -1 for its reverse. In this blog, we discussed the methods to compare doubles in Java.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.