Java 128陷阱是指:当基本类型数据的封装类保存的值大于等于128的时候,每个类的地址都不相等。
简单来讲就是:在小于等于127大于等于-128的时候,自动装箱的是同一个地址存在的数值.大于127时,自动装箱的不是同一个。代码示例:
Integer a = 100;
Integer b = 100;
Integer c = 200;
Integer d = 200;
System.out.println(a == b);
//输出true
System.out.println(c == d);
//输出false
一般来说,Java中的类使用 “==” 的时候比较的都是地址,那为什么以上代码中a和b的比较能输出true呢,其实这就是引发Java 128陷阱的关键:
在Java中,Interger是数据类型int的封装类,Java设计者是这么认为的:
每次都要开辟新空间会占用大量的资源,因此他们规定在-128~127(因为Java设计者认为大家对数的使用大多在100以内)之间的Integer类型的变量,直接指向常量池中的缓存地址,不会new开辟出新的空间
由于自动拆装箱的存在,使得Integer类型的数据能够和基本类型int类型的数据进行比较,此场景下数据大小的判断与Java 128陷阱无关,也是需要注意的一点,代码示例:
Integer a = 100;
Integer b = 200;
int c = 100;
int d = 200;
System.out.println(a==c);
//输出true
System.out.println(b==d);
//输出true
因此,在要比较Integer类型中数据时,尤其是可能大于等于128的数据,需要先通过Integer的intValue()方法进行拆箱,否则会出错,代码示例:
Integer a1 = 128;
Integer a2 = 128;
Integer b1 = 128;
Integer b2 = 128;
System.out.println(a1.intValue() == a2.intValue());
//输出true
System.out.println(b1 == b2);
//输出false
在Java的Integer类的源码中,新创建一个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 (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() {}
}
判断存储数据的大小是否在-127~128的范围内,如果在,那么将准备好的地址返回:
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
如果不在,那么新建一个Integer对象
public Integer(int value) {
this.value = value;
}
有关Java 128陷阱更多的代码示例:
public static void main(String[] args) {
int a1 = 30;
int a2 = 30;
// 值比较:true
System.out.println(a1 == a2);
int b1 = 130;
int b2 = 130;
// 值比较:true
System.out.println(b1 == b2);
Integer c1 = 30;
Integer c2 = 30;
// 类比较,但在-127~128之间,属于常量:true
System.out.println(c1 == c2);
Integer d1 = 130;
Integer d2 = 130;
// 类比较,不在-127~128之间,属于新建对象:false
System.out.println(d1 == d2);
Integer e1 = 130;
Integer e2 = 130;
// 类转为数值比较:true
System.out.println(e1.intValue() == e1.intValue());
Integer f1 = 130;
int f2 = 130;
// f1自动拆箱,不涉及128陷阱,与f2比较:true
System.out.println(f1 == f2);
long g1 = 30;
long g2 = 30;
// 值比较:true
System.out.println(g1 == g2);
long h1 = 130;
long h2 = 130;
// 值比较:true
System.out.println(h1 == h2);
Long i1 = 30l;
Long i2 = 30l;
// 类比较,但在-127~128之间,属于常量:true
System.out.println(i1 == i2);
Long g1 = 130l;
Long g2 = 130l;
// 类比较,不在-127~128之间,属于新建对象:false
System.out.println(g1 == g2);
Long k1 = 130l;
Long k2 = 130l;
// 类转为数值比较:true
System.out.println(k1.longValue() == k2.longValue());
Integer l1 = 130l;
int l2 = 130l;
// l1自动拆箱,不涉及128陷阱,与l2比较:true
System.out.println(l1 == l2);
}