static { // high value may be configured by property inth=127; //读取JVM参数值 StringintegerCacheHighPropValue= sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high"); //初始化high if (integerCacheHighPropValue != null) { try { inti= parseInt(integerCacheHighPropValue); i = Math.max(i, 127); // Maximum array size is Integer.MAX_VALUE h = Math.min(i, Integer.MAX_VALUE - (-low) -1); } catch( NumberFormatException nfe) { // If the property cannot be parsed into an int, ignore it. } } high = h; //127 - (-128) + 1为数组容量 cache = newInteger[(high - low) + 1]; //初始化Cache数组 intj= low; for(intk=0; k < cache.length; k++) cache[k] = newInteger(j++);
// range [-128, 127] must be interned (JLS7 5.1.7) assert IntegerCache.high >= 127; }
privateIntegerCache() {} }
Integer.valueOf()
首先看一下关于该方法的实现:
1 2 3 4 5 6 7 8 9 10
publicstatic Integer valueOf(int i) { //判断传入的变量是否处于IntegerCache之内,是的话直接返回缓存中的数据 if (i >= IntegerCache.low && i <= IntegerCache.high) return IntegerCache.cache[i + (-IntegerCache.low)]; returnnewInteger(i); }