Here no boxing or unboxing need to happen so it doens't happen. Generally when
you compare objects using '==' it checks for reference equality rather its value
equality(done through equals() method of object). But then how i==j returns true
? As String pool even Integer has its own pool or cache.
static
final Integer cache[] = new Integer[-(-128) + 127 + 1];
static
{ for(int i = 0; i < cache.length; i++) cache[i] = new
Integer(i - 128); }
From -128 to 127, values available in the the
pool already, it takes from the pool. And for the values above/below it will
create new object in the heap. Do the same operation for any value above 127 you
will get false as the result.
Try the below snippet.
public
class IntegerValueCheck {
public static void main(String[
] args)
{ Integer i = 127; Integer j = 127;
System.out.println("i==j >> "+ (i==j));
Integer a = 127; Integer b = new Integer(127);
System.out.println("a==b >> "+ (a==b));
Integer k = 128; Integer l = 128;
System.out.println("k==l >> "+ (k==l));
}
}
i==j >> true a==b >> false k==l
>> false
|