在说缓存池之前先聊一个内容,就是关于“==”
先看一段代码
Integer int_01 = 123;
Integer int_02 = 123;
System.out.println(int_01 == int_02); //true
Integer int_03 = new Integer(123);
Integer int_04 = new Integer(123);
System.out.println(int_03 == int_04); //false
对于 “ == ”号咱么都知道,当 “== ”比较的是引用对象的时候,其实比较的是两个对象的内存地址,如果是比较基本类型数据的时候比较的是具体值。
通过debug可以看到int_01和int_02的内存地址是一样的,但是当我们使用new Integer(),等于又重新创建了一个对象,所以int_03和int_04的内存地址就是不同的。
拿Integer来举列子,咱们获得一个Integer对象有以下几种方法:
Integer a = 123;
Integer b = new Integer(123);
Integer c = Integer.valueOf(123);
但是abc三个的内存地址是否呢?
咱们直接比较一下,可以得出下面的结果:
Integer a = 123;
Integer b = new Integer(123);
Integer c = Integer.valueOf(123);
System.out.println(a == b); //false
System.out.println(a == c); //true
System.out.println(b == c); //false
发现a和c的内存地址竟然一样!这是为啥啊?!
OK,现在进入正题了!到此为止,正文才开始,这就关系到了我们说的缓存池。
缓存池
咱们啊不妨进源码里瞅瞅,点进"valueOf"的方法里,可以看到下面这段代码:
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
valueOf() 方法的实现比较简单,就是先判断值是否在缓存池中,如果在的话就直接返回缓存池的内容。
在JDK1.8中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 =
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() {}
}
所以当我们传入一个在-128到127之间的整数时,编译器会在缓冲池中取出整数对应的Integer对象,因此多个 Integer 实例使用自动装箱来创建并且值相同,那么就会引用相同的对象,否则会创建一个新的对象。比如下面这样:
Integer a = 123;
Integer c = Integer.valueOf(124);
System.out.println(a == c); //false