Hold住面試官之Integer Cache
最近跟許多朋友聊了下,在這個“跳槽”的黃金季節,大家都有點蠢蠢欲動,所以最近就多聊聊面試的時候需要注意的一些問題,這些問題不一定多深奧,多複雜,但是一不注意的話卻容易掉坑。下面看一下面試的時候經常遇到的一段程式碼:
public class IntegerDemo { public static void main(String[] args) { Integer numA = 127; Integer numB = 127; Integer numC = 128; Integer numD = 128; System.out.println("numA == numB : "+ (numA == numB)); System.out.println("numC == numD : "+ (numC == numD)); } } 複製程式碼
根據大家以往的經驗,會認為上面的程式碼用“==“符號來比較,對比的是物件的引用,那麼ABCD是不同的物件,所以輸出當然是false了。我在 《“==”、“equals()”、“hashcode()”之間的祕密 》這篇文章也討論過。那麼事實也是如此嗎?下面看一下輸出結果:
numA == numB : true numC == numD : false 複製程式碼
What?這個輸出結果怎麼跟以往的認知有所出入呢?在我們的程式碼“Integer numA = 127”中,編譯器會把基本資料的“自動裝箱”(autoboxing)成包裝類,所以這行程式碼就等價於“Integer numA = Integer.valueOf(127)”了,這樣我們就可以進入valueOf方法檢視它的實現原理。
//Integer valueOf方法 public static Integer valueOf(int i) { if (i >= IntegerCache.low && i <= IntegerCache.high) return IntegerCache.cache[i + (-IntegerCache.low)]; return new Integer(i); } //Integer靜態內部類 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() {} } 複製程式碼
從上面的原始碼可以看到,valueOf方法會先判斷傳進來的引數是否在IntegerCache的low與high之間,如果是的話就返回cache數組裡面的快取值,不是的話就new Integer(i)返回。
那我們再往上看一下IntegerCache,它是Integer的內部靜態類,low預設是-128,high的值預設127,但是high可以通過JVM啟動引數XX:AutoBoxCacheMax=size來修改(如圖),如果我們按照這樣修改了,然後再次執行上面程式碼,這時候2次輸出都是true,因為快取的區間變成-128~200了。

但是如果我們是通過構造器來生成一個Integer物件的話,下面的輸出都是false。因為這樣不會走ValueOf方法,所以按照正常的物件對比邏輯即可。
public class IntegerDemo { public static void main(String[] args) { Integer numA = new Integer(127); Integer numB = new Integer(127); Integer numC = new Integer(128); Integer numD = new Integer(128); System.out.println("numA == numB : "+ (numA == numB));//false System.out.println("numC == numD : "+ (numC == numD));//false } } 複製程式碼
還有需要注意的一點是,此類快取行為不僅存在於Integer物件。還存在於其他的整數型別Byte,Short,Long,Character。但是能改變快取範圍的就只有Integer了。
結語
有時候往往越簡單的知識越容易掉坑裡,所以要保持自己的求知慾,不斷鞏固的基礎,才能讓自己在面試的時候不會栽跟頭。

喜歡的話~關注一下微信公眾號《深夜裡的程式猿》,每天分享最乾的乾貨