先看一段測試:
1 public class TestInteger { 2 public static void main(String[] args) { 3 int sum = 0; 4 for (int i = -128; i <= 127; i++) { 5 Integer j = i; // 自動裝箱 6 Integer k = i; 7 if(j == k) sum++; 8 } 9 System.out.println(sum); // 256 10 Integer a = -129, b = -129; 11 Integer c = 128, d = 128; 12 System.out.println("a == b: " + (a == b)); // false 13 System.out.println("c == d: " + (c == d)); // false 14 // Integer 在與 int類型的值在比較的時候 是用自身的value進行比較 15 Integer e = 2000; 16 int f = 2000; 17 System.out.println("e == f: " + (e == f)); // true 18 } 19 }
從上面的代碼可以看出,在創建-128 ~ 127之間的Integer對象,value相同的Integer對象是同一個對象。
這是由於Integer的緩存機制起的作用。
Integer的緩存機制:為了節省內存和提高性能,Integer類在內部通過使用相同的對象引用實現緩存和重用,Integer類默認在-128 ~ 127 之間,可以通過 -XX:AutoBoxCacheMax進行修改,且這種機制僅在自動裝箱的時候有用,在使用構造器創建Integer對象時無用。
public class TestInteger02 { public static void main(String[] args) { int sum = 0; for (int i = -128; i <= 127; i++) { Integer j = new Integer(i); Integer k = new Integer(i); if (j == k) sum++; } System.out.println(sum); // 結果是 0 } }
自動裝箱:將基本數據類型轉換為包裝器類型
自動拆箱:將包裝器類型裝換為基本數據類型
Integer j = 20; 自動裝箱,相當於調用Integer.valueOf(20),首先會判斷 20 是否在 -128~127之間,如果在就直接從IntegerCache.cache緩存中獲取指定數字的包裝類,否自new一個Integer對象。
public static Integer valueOf(int i) { if (i >= IntegerCache.low && i <= IntegerCache.high) return IntegerCache.cache[i + (-IntegerCache.low)]; return new Integer(i); }
// IntergerCache是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() {} }
自動裝箱與自動拆箱的實現機制: 享元模式(復制內存中已存在的對象,降低系統創建對象實例的內存)