It caches the int
value -128 and 127 (inclusive), So it will refer to same instance in memory within that range
When you pass primitive value to (here 10
)
System.identityHashCode(10);
It autoboxes it to Integer
object and it inturns uses valueOf()
method of Integer class for conversion
For Example
Integer a = 10;
will get converted in to internally it uses valueOf()
1: invokestatic #2; //Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
Integer.valueOf()
which has got the cache implementation
public static Integer valueOf(int i) {
if(i >= -128 && i <= IntegerCache.high)
return IntegerCache.cache[i + 128];
else
return new Integer(i);
}
so if you pass the value from -128 to 127(inclusive) it will use the cached version as you can see from
if(i >= -128 && i <= IntegerCache.high)
return IntegerCache.cache[i + 128];
See Also