当我们写Integer i = 126,实际上做了自动装箱:Integer i = Integer.valueOf(126); 分析这段源码
1 2 3 4 5 6 7 8 9 10 11 12 13
publicstatic Integer valueOf(int i){ if (i >= IntegerCache.low && i <= IntegerCache.high) return IntegerCache.cache[i + (-IntegerCache.low)]; returnnew Integer(i); } //IntegerCache是Integer的内部类 privatestaticclassIntegerCache{ staticfinalint low = -128; staticfinalint high; staticfinal Integer cache[]; static { // high value may be configured by property int h = 127;