這是我們今天要討論的話題,因爲我覺得它非常的有趣。
如果你運行如下代碼:
Integer a = 1000, b = 1000;
System.out.println(a == b);//1
Integer c = 100, d = 100;
System.out.println(c == d);//2
你會得到以下運行結果:
false
true
我們知道,如果兩個引用指向同一個對象,那麼==就成立;反之,如果兩個引用指向的不是同一個對象,那麼==就不成立,即便兩個引用的內容是一樣的。因此,結果就會出現false。
這是非常有趣的地方。如果你查看Integer.java類,你會找到IntegerCache.java這個內部私有類,它爲-128到127之間的所有整數對象提供緩存。
這個東西爲那些數值比較小的整數提供內部緩存,當進行如此聲明時:
Integer c = 100;
它的內部就是這樣的:
Integer i = Integer.valueOf(100);
如果我們觀察valueOf()類函數,我們可以看到
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
如果值在-128到127之間,它就會返回該緩存的實例。
因此:
Integer c = 100, d = 100;
兩者指向同樣的對象。
這就是爲什麼這段代碼的結果爲true了:
System.out.println(c == d);
現在你可能會問,爲什麼會爲-128到127之間的所有整數設置緩存?
這是因爲在這個範圍內的小數值整數在日常生活中的使用頻率要比其它的大得多,多次使用相同的底層對象這一特性可以通過該設置進行有效的內存優化。你可以使用reflection API任意使用這個功能。
運行下面的這段代碼,你就會明白它的神奇所在了。
public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
Class cache = Integer.class.getDeclaredClasses()[0]; //1
Field myCache = cache.getDeclaredField("cache"); //2
myCache.setAccessible(true);//3
Integer[] newCache = (Integer[]) myCache.get(cache); //4
newCache[132] = newCache[133]; //5
int a = 2;
int b = a + a;
System.out.printf("%d + %d = %d", a, a, b); //
}