测试代码样式

前几天,有个同事问了我一个关于Integer类赋值的问题,很有意思,我们一起来看一下(如果有说的不正确的地方,欢迎大家指正)。

image_thumb1_thumb
如上图,同样是赋值,但是两次比较的结果完全不同。我们走近了解一下。
在Integer中,有一个静态内部类IntegerCache,其内有一个Integer缓存数组,范围从-128到127
private static class IntegerCache {
static final int low = -128;
static final int high;
static final Integer cache[];

static {
// high value may be configured by property
int h = 127;
String integerCacheHighPropValue =
sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
        //此处if是处理VM入参,可忽略
if (integerCacheHighPropValue != null) {
try {
int i = 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;
//构造一个长度是256的Integer数组
cache = new Integer[(high - low) + 1];
int j = low;
        //遍历赋值-128到127
for(int k = 0; k < cache.length; k++)
cache[k] = new Integer(j++);

// range [-128, 127] must be interned (JLS7 5.1.7)
assert IntegerCache.high >= 127;
}

private IntegerCache() {}
}
当执行赋值语句的时候,会调用Integer的valueOf方法,此方法会判断入参大小,如

猜你喜欢

转载自www.cnblogs.com/wangl110/p/10753733.html