tags:

views:

171

answers:

6
+9  Q: 

java == operator

Possible Duplicate:
Weird Java Boxing

Hi,

Can somebody explain why the last print returns false ?

int a = 100;
int b = 100;

System.out.println(a == b); // prints true

Integer aa = 100;
Integer bb = 100;

System.out.println(aa == bb); // prints true

Integer aaa = 1000;
Integer bbb = 1000;

System.out.println(aaa == bbb); // prints false

Thanks Michael

+1  A: 

You're comparing two Integer objects, which using the == operator compares the two references, instead of the two values.

Use the equals() method to be sure you're comparing both values.

StudiousJoseph
what abput `System.out.println(aa == bb);`
aaa
-1 since aa == bb prints true.
Tushar Tarkas
+1 because this is all correct, even if it doesn't fully explain.
Justin Ardini
A: 

You must use Integer.compareTo for testing numerical equality for Integer objects. The == operator is comparing the objects, not the numbers.

bshields
You can use Integer.equals as well.
Yishai
+12  A: 

The reason why the second print evaluates to true is because the first 128 Integer objects are cached by the Integer class. You want to use .equals

Amir Afghani
Yeah, this is the right answer. See this also: http://www.owasp.org/index.php/Java_gotchas "Immutable Objects/Wrapper Class Caching"
Andy White
This is probably my number one disliked "feature" of Java. Talk about inconsistencies.
Zachary
I would really like to be able to explicitly disable initialization autoboxing.
Thorbjørn Ravn Andersen
I suspect they did this for performance. If you stick to using valueOf you won't get stung, but I agree it's inconsistent.
Amir Afghani
IMO, anyone who uses `==` to compare values of *any* `Object` is asking to get burned.
Justin Ardini
Oh but we're so clever with String interning and == and...I smell smoke.
Amir Afghani
A: 

The Integer objects are autoboxed using the method Integer.valueOf(int). Have a look at the documentation of that method. Then everything should become clear.

Roland Illig
A: 

What you want to be using is aaa.equals(bbb). In Java using == on objects compares whether they are the same instance of the object instead of whether they are equal according to their equals() method.

dave
A: 

I'm surprised the second case return true. But that why in Java Puzzlers they advise against mixing the use of Wrapper classes and the == operator.

Take a look at this class and code:

public class RichardInt {
    private int value;
    public RichardInt(int value) {
        this.value = value;
    }
}

What would be the result of the following?

RichardInt aa = new RichardInt(100);
RichardInt bb = new RichardInt(100);
System.out.println(aa == bb); // prints false

It prints false because the equals operator == compares references when used with objects. Remember, in Java, objects are reference types, while primitives (like int) are value types. (note: can someone let me know if I'm misusing "value type"? I have a feeling I am.) The following would print true:

RichardInt aa = new RichardInt(100);
RichardInt bb = aa;
System.out.println(aa == bb); // prints true

... since aa and bb reference the same instance of RichardInt.

So maybe the above behavior is easier to understand in the following example...

Intger aaa = new Intger(1000);
Intger bbb = new Integer(1000);
System.out.println(aaa == bbb); // prints false

In more recent versions of Java, the wrapper classes (Integer and Float and Boolean etc...) can auto-box, meaning you can do the following:

Integer aa = 1000;
// this is a shorthand for...
// Integer aa = new Integer(1000);

But it leads to confusing behavior when you try things like this:

Integer aaa = 1000;
Integer bbb = 1000;
System.out.println(aaa == bbb); // prints false

Or even better, you can end up with puzzles like this...

// what values of x and y will result in output?
if(!(x<y)&&!(x>y)&&!(x==y)) {
    System.out.println("How is this possible?");
}

In the end, whenever dealing with objects, you'll want to use .equals() instead of ==.

Integer aaa = 1000;
Integer bbb = 1000;
System.out.println(aaa.equals(bbb)); // prints true
LeguRi