tags:

views:

179

answers:

3
class D
{
public static void main(String args[])
                {
                    Integer b2=128;
                    Integer b3=128;
                    System.out.println(b2==b3);

                }

}

output ::::false


class D
{
public static void main(String args[])
                {
                    Integer b2=127;
                    Integer b3=127;
                    System.out.println(b2==b3);

                }

}

output ::::true

Note: Numbers between -128 to 127 are true.

+4  A: 

Autoboxing caches -128 to 127. This is specified in the JLS (5.1.7).

If the value p being boxed is true, false, a byte, a char in the range \u0000 to \u007f, or an int or short number between -128 and 127, then let r1 and r2 be the results of any two boxing conversions of p. It is always the case that r1 == r2.

A simple rule to remember when dealing with objects is - use .equals if you want to check if the two objects are "equal", use == when you want to see if they point to the same instance.

mlk
+17  A: 

when you compile a number literal in java and assign it to a Integer (capital I) the compile emits

Integer b2 =Integer.valueOf(127)

this line of code is also generated when you use autoboxing.

valueOf is implemented such that certain numbers are "pooled" and it returns the same instance for values smaller than 128.

from the java 1.6 source code, line 621:

 public static Integer valueOf(int i) {
    if(i >= -128 && i <= IntegerCache.high)
        return IntegerCache.cache[i + 128];
    else
        return new Integer(i);
}

the value of high can be configured to another value, with the system property

-Djava.lang.Integer.IntegerCache.high=999

if you run your program with that system property, it will output true!!

the obvious conclusion: never rely on two references being identical, always compare them with .equals() method.

so b2.equals(b3) will print true for all logically equal values of b2,b3.

note that is Integer cache is not there for performance reasons, but rather to comform to the JLS, section 5.1.7, that object identity must be given for values -128 to 127 inclusive.

Andreas Petersson
+1 I was not aware you could fiddle with the size of the integer cache.
mlk
Good lord... an integer cache.
izb
note that values smaller than 127 will be ignored by java and values bigger than Integer.MAX_VALUE-128 will be capped.
Andreas Petersson
Now - is that making it easier?
RED SOFT ADAIR
Integers are cached for byte values in Java 5 and higher, making new Integer(1) == new Integer(1). However, this is not the case in Java 1.4 or lower, so beware if you have to eventually downgrade to that environment.
MetroidFan2002
no, this is wrong. new Integer(1) == new Integer(1) is false regardless of the jvm. AFAIK no compiler will cheat at the "new" keyword. it MUST always instantiate a new object.
Andreas Petersson
+1  A: 

Using primitive data types, ints, would produce true in both cases, the expected output.

However, since you're using Integer objects the == operator has a different meaning.

In the context of objects, == checks to see if the variables refer to the same object reference.

To compare the value of the objects you should use the equals() method E.g.

 b2.equals(b1)

which will indicate whether b2 is less than b1, greater than, or equal to (check the API for details)

chrisbunney