记录一次integer比较的失误。
背景:
两个integer类型的数据在比较时,使用了==;比较结果为true(前提数值一样)。代码片段如下:
Integer id = sp.getId();
List<String> subResource = new ArrayList<>();
for (SysResource ss : subList) {
if (id == ss.getParentId()) {
subResource.add(ss.getPath());
}
}
只需判断id和ss.getParentId() 是否相等;
开发环境测试时,达到期望结果;但是在线上环境,发现subResource为空;进一步跟进数据,发现明明id和ss.getParentId()有一样的数值,但是if判断false
对于上面的现象,我的问题是:integer数据在使用==比较时为什么有的为true,有的为false(数值一样的前提下)?
integer是int的包装类型,在对integer对象赋值时,使用了IntegerCache
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) {
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;
cache = new Integer[(high - low) + 1];
int j = low;
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() {}
}
IntegerCache由三部分组成:low、hign、cache;low= -128,high=127,cache是一个在-128~127之间的数组。每次对integer对象赋值的时候,都会判断数值大小,如果数值在-128~127之间,就会把数据存储在这个缓存中,下次用到的时候再从缓存中获取,也就是说当两个数值为90的integer比较的时候,他们获取的是同一个对象!一旦数值超过这个区间,就会在JVM中开辟空间存储,这个时候对象地址就是不同的了。
比较了一下,之前在开发环境的数值,id=126;但是线上id=130;当id=126时都从缓存对象中取值,可以使用==比较,这时纯比较数值;当id=130时,他们在JVM中有各自的对象地址;再次使用==比较时,这时是比较对象地址,当然返回false。
所以,不要抱着侥幸心理;包装对象比较使用equals更安全。