平时我看到很多人写的代码比较Integer都是习惯性用==,在测试的时候基本没错,因为测试的数据少,基本就几条,id也少。基本在-128到127之间,如果数据多了,超出了这个范围就会出错的。
static final Integer cache[] = new Integer[-(-128) + 127 + 1];
static {
for(int i = 0; i < cache.length; i++)
cache[i] = new Integer(i - 128);
}
上面是源码;
建议比较Integer还是使用equals,或者Integer.intValue()得带int值在进行比较.