Part 1.创建Integer
//话不多说,Integer==1;与Integer j=1000;源码执行如下
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
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;
如此,关于Integer i =1; Integer j=1;这两个数相等或者Integer i=1000;是否相等的问题就一目了然了
Part 2.Integer运算 Integer i=200; Integer i2=300; Integer i3=500; i3==i+i2的结果肯定是true
运算会涉及到自动拆装箱,进行的是值得比较
Integer==陷阱
最新推荐文章于 2024-03-26 09:35:58 发布