1. 问题
- Integer 中为什么用 == 比较地址结果会不同?
- 下例中,为什么
i1 == i2
是 false,i3 == i4
却是 true?
public class Test {
public static void main(String[] args) {
Integer i1 = 250;
Integer i2 = 250;
Integer i3 = 250;
Integer i4 = 250;
System.out.println(i1==i2);//false
System.out.println(i1.equals(i2));//true
System.out.println(i3==i4);//true
System.out.println(i3.equals(i4));//true
}
}
2. 解答
- Integer 类存在 IntegerCache 缓存类,其默认缓存了 -128 ~ 127 的Integer 值,如遇到 [-128,127] 范围的值需要转换为 Integer 时会直接从 IntegerCache 中获取;
- 为什么要做这种缓存 [-128,127] 呢?
- [-128,127] 是 byte 的取值范围,在 IO 流中,每次读取文件,最基本的单位是 1 byte,使用 IntegerCache 缓存类能在自动装箱的时候不会创建大量对象;
- 参考 API 如下:
/**
* Cache to support the object identity semantics of autoboxing for values between
* -128 and 127 (inclusive) as required by JLS.
*
* The cache is initialized on first usage. The size of the cache
* may be controlled by the {@code -XX:AutoBoxCacheMax=<size>} option.
* During VM initialization, java.lang.Integer.IntegerCache.high property
* may be set and saved in the private system properties in the
* jdk.internal.misc.VM class.
*
* WARNING: The cache is archived with CDS and reloaded from the shared
* archive at runtime. The archived cache (Integer[]) and Integer objects
* reside in the closed archive heap regions. Care should be taken when
* changing the implementation and the cache array should not be assigned
* with new Integer object(s) after initialization.
*/
private static class IntegerCache {
static final int low = -128;
static final int high;
static final Integer[] cache;
static Integer[] archivedCache;
static {
// high value may be configured by property
int h = 127;
String integerCacheHighPropValue =
VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
if (integerCacheHighPropValue != null) {
try {
h = Math.max(parseInt(integerCacheHighPropValue), 127);
// Maximum array size is Integer.MAX_VALUE
h = Math.min(h, Integer.MAX_VALUE - (-low) -1);
} catch( NumberFormatException nfe) {
// If the property cannot be parsed into an int, ignore it.
}
}
high = h;
// Load IntegerCache.archivedCache from archive, if possible
VM.initializeFromArchive(IntegerCache.class);
int size = (high - low) + 1;
// Use the archived cache if it exists and is large enough
if (archivedCache == null || size > archivedCache.length) {
Integer[] c = new Integer[size];
int j = low;
for(int i = 0; i < c.length; i++) {
c[i] = new Integer(j++);
}
archivedCache = c;
}
cache = archivedCache;
// range [-128, 127] must be interned (JLS7 5.1.7)
assert IntegerCache.high >= 127;
}
private IntegerCache() {}
}