noFearOfBeer
noFearOfBeer

Reputation: 87

Double comparison failing in Java

Why does the first if-statement return true and the second false?

Isn't 100000000000000.032 in the first if-statement also turned into a new Double that is different to dd?

Double dd = 100000000000000.032;
if(dd == 100000000000000.032) {
    System.out.println("gaga");
}

Double ee = 100000000000000.032;
if(dd == ee) {
    System.out.println("gaga");
}

Upvotes: 0

Views: 573

Answers (5)

Bogdan Alexandru
Bogdan Alexandru

Reputation: 11

In the first if: - you use == to test an object with a primitive; the object is converted to primitive and the two are equal. In the second if: - you compare two different objects.

Try ee == ee and see that it returns true.

Upvotes: 1

tquadrat
tquadrat

Reputation: 4054

Either you follow Majed Badawi's answer to use equals() because you compare object instances, or you modify your code like below:

double dd = 100000000000000.032
if( dd == 100000000000000.032 ) 
{
    System.out.println( "gaga" );
}

double ee = 100000000000000.032;
if( dd == ee ) 
{
    System.out.println( "gaga" );
}

Note that dd and ee are now of the primitive type double (with a lowercase 'D').

In your sample, the first comparison worked because internally, it was interpreted as:

… 
if( dd.doubleValue() == 100000000000000.032 ) 
…

This behaviour is called 'Autoboxing/-unboxing' and was introduced with Java 5; for versions before Java 5, your code would not compile at all.

Upvotes: 0

dutoitns
dutoitns

Reputation: 2273

== tests if the objects are the same object, not that they have the same value. In your example dd and ee are distinct objects even though they have the same value. So if you want to compare their values you will need to use the equals method.

Here is a small test to help you get a handle on it:

package stackoverflow;

import org.junit.jupiter.api.Test;

public class QuickTest {

    @Test
    public void test() throws Exception {
        Double number1 = 100000000000000.032;
        Double number2 = 100000000000000.032;

        System.out.println("Do they point to the same object? " + (number1 == number2));

        System.out.println("Are they equal, do they have the same value? " + (number1.equals(number2)));

        Double number3 = 100000000000000.032;
        Double number4 = number3;

        System.out.println("Do they point to the same object? " + (number3 == number4));

        System.out.println("Are they equal, do they have the same value? " + (number3.equals(number4)));
    }
}

This prints out the following:

Do they point to the same object? false
Are they equal, do they have the same value? true
Do they point to the same object? true
Are they equal, do they have the same value? true

Upvotes: 0

Kaushik Galiya
Kaushik Galiya

Reputation: 19

Double is object so == won't give required result, use .equals method

Upvotes: 0

Majed Badawi
Majed Badawi

Reputation: 28434

Since you are comparing two objects in the second if-statement, you should be using the equals method as follows:

if(dd.equals(ee))

Upvotes: 2

Related Questions