Integer类里面有一个私有的静态内部类IntegerCache类加载时,有一段静态块代码,如下

        static final int low = -128;
static final int high;
static final Integer cache[];
static {
// high value may be configured by property
int h = ;
String integerCacheHighPropValue =
sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
if (integerCacheHighPropValue != null) {
try {
int i = parseInt(integerCacheHighPropValue);
            //如果配置了IntegerCache的长度,取它和127的最大值
i = Math.max(i, );
// Maximum array size is Integer.MAX_VALUE
            h=min(i,0x7fffffff-128-1)的最小值
h = Math.min(i, Integer.MAX_VALUE - (-low) -);
} catch( NumberFormatException nfe) {
// If the property cannot be parsed into an int, ignore it.
}
}
       //赋值给high
high = h;
       //如果没配置,127-(-128)+1=256
       //-128到0//129个元素
       //1到127//127个元素
cache = new Integer[(high - low) + ];
      //low等于-128
int j = low;
for(int k = ; k < cache.length; k++)
cache[k] = new Integer(j++);
        //-128,127的数一定在cache数组里面,
// range [-128, 127] must be interned (JLS7 5.1.7)
assert IntegerCache.high >= ;
} private IntegerCache() {}

Integer的valueOf方法

    public static Integer valueOf(int i) {
      //默认情况下i>=-128&&i<=127,返回cache数组里面的东西
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}

  那么通过设置

java.lang.Integer.IntegerCache.high
可以调大IntegerCache的缓存数字咯.
	int q = 10;
q = (q << 6) + (q << 5) + (q << 2);//q=1000
getChars方法里面,这个段代码,把q的值乘以100
  

  

05-11 22:50