您的位置:首页 > 产品设计 > UI/UE

Interger声明的在-128 ~ 127之间值比较为true

2016-03-25 11:02 295 查看
Integer f1=100,f2=100,f3=189,f4=189;
System.out.println(f1==f2);
System.out.println(f3==f4);

输出 true false

源码:

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 (integerCacheHighPropValue != null) {
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);
}
high = h;

cache = new Integer[(high - low) + 1];
int j = low;
for(int k = 0; k < cache.length; k++)
cache[k] = new Integer(j++);
}

private IntegerCache() {}
}

Interger 实现的时候 对于-128 到127之间的值做了缓存处理 (IntegerCache ),源码见上。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: