You must understand this method from java.lang.Integer
public static Integer valueOf(int i) {
assert IntegerCache.high >= 127;
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
In the first case, you create a new object Integer, and in the second case, the compiler performs the conversion for you using the cache.
Here is the corresponding bytecode to find out how it ends with a call to the constructor Integeror Integer.valueOf:
0: new #2; //class java/lang/Integer
3: dup
4: iconst_3
5: invokespecial #3; //Method java/lang/Integer."<init>":(I)V
8: astore_1
9: aload_1
10: astore_2
11: iconst_4
12: anewarray #2; //class java/lang/Integer
15: dup
16: iconst_0
17: iconst_1
18: invokestatic #4; //Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
21: aastore
...
90: iconst_3
91: istore 6
93: iload 6
95: invokestatic #4; //Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
98: astore 7
100: iconst_4
101: anewarray #2; //class java/lang/Integer
104: dup
105: iconst_0
106: iconst_1
107: invokestatic #4; //Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
110: aastore
111: dup
...
source
share