首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 程式設計入門

Java 裝箱拆箱深度解析:Integer 快取機制、== 與 equals 陷阱全攻略

     編輯:Java 程式設計入門

自動裝箱拆箱是 JDK 1.5 引入的語法糖,極大簡化了基本型別與包裝類轉換,但也引入了經典面試陷阱。本文深度剖析 Integer.valueOf() 快取機制、== 比較行為、算術運算觸發拆箱等核心知識點。

核心概念

  • 裝箱:基本型別 → 包裝類(如 int → Integer),調用 Integer.valueOf(int)
  • 拆箱:包裝類 → 基本型別(如 Integer → int),調用 Integer.intValue()

Integer 快取機制源碼解析

public static Integer valueOf(int i) {
    if (i >= IntegerCache.low && i <= IntegerCache.high)
        return IntegerCache.cache[i + (-IntegerCache.low)];
    return new Integer(i);
}

// IntegerCache 內部類
static final int low = -128;
static final int high = 127; // 可透過 -XX:AutoBoxCacheMax 調整上限
static final Integer cache[] = new Integer[(high - low) + 1];
static {
    for (int k = 0; k < cache.length; k++)
        cache[k] = new Integer(low + k);
}

結論:-128 ~ 127 之間的整數直接返回快取對象,超出範圍才 new 新對象。

經典面試題實戰演練

Integer i1 = 100;  // 裝箱 → valueOf(100) → 快取
Integer i2 = 100;  // 快取同一對象
Integer i3 = 200;  // 裝箱 → valueOf(200) → new Integer(200)
Integer i4 = 200;  // 新對象

System.out.println(i1 == i2); // true  同一快取對象
System.out.println(i3 == i4); // false 不同 new 對象
System.out.println(i1 == 100); // true  100 是 int,觸發拆箱比較數值
System.out.println(i3 == 200); // true  同理拆箱比較數值
System.out.println(i1.equals(i2)); // true  比較數值
System.out.println(i3.equals(i4)); // true  比較數值

關鍵規則總結

表達式行為結果
包裝類 == 包裝類比較引用地址快取範圍內 true,否則 false
包裝類 == 基本型別觸發拆箱,比較數值數值相等則 true
包裝類.equals(包裝類)比較數值(已重寫)數值相等則 true
算術運算(+ - * /)自動拆箱為基本型別運算結果為基本型別

其他包裝類快取範圍

  • Byte、Short、Long:全範圍快取(-128~127)
  • Character:0~127 快取
  • Boolean:TRUE、FALSE 兩個單例
  • Float、Double:無快取(浮點數值無限,不可快取)

最佳實踐建議

  1. 比較數值務必用 equals() 或拆箱後用 ==
  2. 避免在高頻迴圈中自動裝箱(如 Long sum = 0L; sum += i; 每次裝箱拆箱),改用基本型別
  3. 大量數值運算場景優先用基本型別或專用庫(如 FastDoubleParser)
  4. JVM 參數 -XX:AutoBoxCacheMax=3000 可擴大 Integer 快取上限(下限固定 -128)
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved