問題引入
先看下面這段代碼的輸出:
public class Main {
public static void main(String[] args){
Integer i1 = 127;
Integer i2 = 127;
Integer i3 = Integer.valueOf(127);
Integer i4 = new Integer(127);
Integer i5 = 128;
Integer i6 = 128;
System.out.println(i1 == i2);//true
System.out.println(i1 == i3);//true
System.out.println(i1 == i4);//false
System.out.println(i5 == i6);//false
}
}
是不是與你預期的答案不一致,其實都是Cache的原因,別急,請帶着問題耐心將這篇文章看下去。
預備知識
parseInt方法
返回int基本數據類型,該方法有兩個重載的方法。
parseInt(String s)
public static int parseInt(String s) throws NumberFormatException {
return parseInt(s,10);
}
parseInt(String s, int radix)
意思是求“int radix”進制數“String s”的10進制數是多少.比如parseInt(String s)中默認調用 parseInt(s,10)意思是:求10進制數“s”的10進制數是多少。
valueOf方法
用於將基本數據類型變成對象類型,俗稱數據裝箱。
該方法有三個重載方法,筆者重點介紹valueOf(int),因爲其餘兩個重載方法都間接使用了該方法。
public static Integer valueOf(String s) throws NumberFormatException {
return Integer.valueOf(parseInt(s, 10));
}
裝箱與拆箱機制:
裝箱就是自動將基本數據類型轉換爲包裝器類型,拆箱就是自動將包裝器類型轉換爲基本數據類型。
用一段簡單的代碼片來說明Java自動拆箱與裝箱的實現機制:
public class Main {
public static void main(String[] args){
Integer i = 66;
int n = i;
}
}
爲了更加清楚的瞭解自動拆箱與裝箱的過程,將上面的代碼反編譯後得到下圖:
從反編譯得到的字節碼內容可以看出。在裝箱的時候自動調用的是Integer的valueOf(int)方法,而在拆箱的時候自動調用的是Integer的intValue方法。
IntegerCache緩存
在文章開頭提到,正是因爲Cache的原因導致Integer比較時出現了一些另人費解的結果。下面將找出這個Cache。查閱了JDK1.8的源代碼在Integer類的源碼中發現有一個靜態內部類IntegerCache。IntegerCache有一個靜態的Integer數組,因爲有靜態代碼塊的存在,在類加載時就將-128 到 127 的Integer對象創建了,並填充在cache數組中,一旦程序調用valueOf 方法,如果i的值是在-128 到 127 之間就直接在cache緩存數組中去取Integer對象,從而形成對象服用。至於爲什麼這樣設計:我想JDK設計者應該是想節省內存和提升性能。也許他們在很多的統計中發現這些數值範圍內的值緩存命中率比較高。
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() {}
}
上面這段代碼在JDK源碼中註釋如下:
/**
* 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
* sun.misc.VM class.
*/
大概的意思是說IntegerCache中cache數組的上界high是可以改變的:通過jvm參數[-XX:AutoBoxCacheMax=size]可以改變上界。
回到最初的問題
我們再次回到最初的問題:
public class Main {
public static void main(String[] args){
Integer i1 = 127;
Integer i2 = 127;
Integer i3 = Integer.valueOf(127);
Integer i4 = new Integer(127);
Integer i5 = 128;
Integer i6 = 128;
System.out.println(i1 == i2);//true
System.out.println(i1 == i3);//true
System.out.println(i1 == i4);//false
System.out.println(i5 == i6);//false
}
}
(1)i1與i2發生了裝箱的操作,裝箱操作底層調用的valueOf()方法,valueOf()方法體裏是有緩存操作的(上文有提)。當兩個對象類型使用"=="比較時,比較的是變量的地址值。又因爲 127 命中了IntegerCache緩存(-128~127)中之前創建好的Integer,所以i1與i2指向的是同一個內存。
(2)i1與i3與(1)是同樣的道理,都是命中了IntegerCache緩存。
(3)new Integer(127)不會IntegerCache緩存的機制。Integer的源碼如下:
public Integer(int value) {
this.value = value;
}
(4)128 超出了IntegerCache緩存的命中範圍,於是重新創建了一個新的包裝類型即在內存中開闢了一塊新的空間。
引伸
除了Integer類,以下幾種包裝類也有cache機制:Byte、Short、Long、Character。需要注意的是,這四類的cache[]數組長度都是固定的,不可改變。且Byte、Short、Long的cache範圍相同,[-128,127], Character則爲[0,128]。Boolean取值只有2個,valueOf使用兩個常量來自動裝箱.
private static class ByteCache {
private ByteCache(){}
static final Byte cache[] = new Byte[-(-128) + 127 + 1];
static {
for(int i = 0; i < cache.length; i++)
cache[i] = new Byte((byte)(i - 128));
}
}
private static class CharacterCache {
private CharacterCache(){}
static final Character cache[] = new Character[127 + 1];
static {
for (int i = 0; i < cache.length; i++)
cache[i] = new Character((char)i);
}
}
下面是Boolean類的緩存機制:
/**
* The {@code Boolean} object corresponding to the primitive
* value {@code true}.
*/
public static final Boolean TRUE = new Boolean(true);
/**
* The {@code Boolean} object corresponding to the primitive
* value {@code false}.
*/
public static final Boolean FALSE = new Boolean(false);
public static Boolean valueOf(boolean b) {
return (b ? TRUE : FALSE);
}
注意Boolean取值只有2個,valueOf使用兩個常量來自動裝箱.