10

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

Community
  • 1
  • 1
Michael Ellick Ang
  • 1,128
  • 1
  • 9
  • 18
  • 3
    duplicate: http://stackoverflow.com/questions/3130311/weird-java-boxing – Progman Aug 17 '10 at 20:02
  • 1
    Yes, this is a dupe many times over. Also http://stackoverflow.com/questions/3131136/integers-caching-in-java-closed, http://stackoverflow.com/questions/1514910/when-comparing-two-integers-in-java-does-auto-unboxing-occur – Justin Ardini Aug 17 '10 at 20:04

6 Answers6

14

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
  • 37,814
  • 16
  • 84
  • 124
1

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.

Jose Diaz
  • 5,353
  • 1
  • 31
  • 29
0

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

bshields
  • 3,563
  • 16
  • 16
0

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
  • 40,703
  • 10
  • 88
  • 121
0

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
  • 12,406
  • 10
  • 42
  • 59
0

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
Richard JP Le Guen
  • 28,364
  • 7
  • 89
  • 119