Autoboxing And Boxing

class A
{
public static void main(String[] arg)
{
int i=3;
Integer i1=i;
Integer i2=i;
System.out.println(i1==i2);

int j=128;
Integer j1=j;
Integer j2=j;
System.out.println(j1==j2);
}
}

OutPut:True and False
please give a discription

Hi,

JLS 5.1.7 Boxing Conversion states that "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.".

So, in the code example i1 & i2, both will refer to the same Integer object as a result of the boxing conversion in the assignment. On the other hand, for j1 & j2, since j is greater than 127, both j1 & j2 will refer to unique instances of the Integer object even though the value used to initialize these objects is the same.

HTH
Ashish Hareet