自動裝箱與自動拆箱

自動裝箱

Java中所謂的裝箱通俗點就是:八種基本數據類型在某些條件下使用時,會自動變爲對應的包裝器類型。

如下清單1:

1
2
3
4
5
6
7
8
9
10
11
12
@Test
public void boxingTest() {
  
Integer i1 = 17;
Integer i2 = 17;
  
Integer i3 = 137;
Integer i4 = 137;
  
System.out.println(i1 == i2);
11 System.out.println(i3 == i4);
}

輸出:

1
2
true
false
解釋下清單1第11句輸出true的原因:
當包裝器類型進行“==”比較時,i3會調用Integer.valueOf自動裝箱基本數據類型爲包裝器類型。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
* Returns an {@code Integer} instance representing the specified
* {@code int} value. If a new {@code Integer} instance is not
* required, this method should generally be used in preference to
* the constructor {@link #Integer(int)}, as this method is likely
* to yield significantly better space and time performance by
* caching frequently requested values.
*
* This method will always cache values in the range -128 to 127,
* inclusive, and may cache other values outside of this range.
*
* @param i an {@code int} value.
* @return an {@code Integer} instance representing {@code i}.
* @since 1.5
*/
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}

從源碼中可以看出,Integer對象自動緩存int值範圍在low~high(-128~127),如果超出這個範圍則會自動裝箱爲包裝類。

Note:

  1. Integer、Short、Byte、Character、Long這幾個包裝類的valueOf方法的實現是類似的;
  2. Double、Float的valueOf方法的實現是類似的。
  3. Boolean的valueOf方法的實現是個三目運算,形如`  return (b ? TRUE : FALSE);  `

自動拆箱

Java中所謂的拆箱通俗點就是:八種包裝器類型在某些條件下使用時,會自動變爲對應的基本數據類型。

清單2:

1
2
3
4
5
6
7
8
9
10
11
12
@Test
public void unboxingTest() {
Integer i1 = 17;
int i2 = 17;
  
int i3 = 137;
Integer i4 = 137;
  
System.out.println(i1 == i2);
10 System.out.println(i3 == i4);
  
}

輸出:

1
2
true
true

解釋下清單2第10句輸出true的原因:

當程序執行到第10句時,i4會調用Integer.intValue方法自動拆箱包裝器類型爲基本數據類型。

1
2
3
4
5
6
7
/**
* Returns the value of this {@code Integer} as an
* {@code int}.
*/
public int intValue() {
return value;
}

從源碼可以看出,當包裝器類型和基本數據類型進行“==”比較時,包裝器類型會自動拆箱爲基本數據類型。

清單3內容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@Test
public void unboxingTest() {
Integer i1 = 17;
Integer i2 = 17;
  
Integer i3 = 137;
Integer i4 = 137;
  
// ==
System.out.println(i1 == i2);
System.out.println(i3 == i4);
  
// equals
System.out.println(i1.equals(i2));
15 System.out.println(i3.equals(i4));
  
}

輸出:

1
2
3
4
true
false
true
true

解釋第15句爲什麼會輸出true:

因爲在Integer包裝類實現的equals方法中,只要比較的當前對象是Integer實例,那麼就會自動拆箱爲基本數據類型。從以下Integer類的equals方法的源碼就可看出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/**
* Compares this object to the specified object. The result is
* {@code true} if and only if the argument is not
* {@code null} and is an {@code Integer} object that
* contains the same {@code int} value as this object.
*
* @param obj the object to compare with.
* @return {@code true} if the objects are the same;
* {@code false} otherwise.
*/
public boolean equals(Object obj) {
if (obj instanceof Integer) {
return value == ((Integer)obj).intValue();
}
return false;
}

Note:

  1. Integer、Short、Byte、Character、Long這幾個包裝類的intValue方法的實現是類似的;
  2. Double、Float的intValue方法的實現是類似的。
  3. Boolean的booleanValue方法的實現和intValue方法的實現也是類似的。

裝箱拆箱綜合清單:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public static void main(String args[]) {
  
Integer a = 1;
Integer b = 2;
Integer c = 3;
Integer d = 3;
Integer e = 321;
Integer f = 321;
  
Long g = 3L;
Long h = 2L;
  
// 會自動拆箱(會調用intValue方法)
System.out.println(c==d);
// 會自動拆箱後再自動裝箱
System.out.println(e==f);
// 雖然“==”比較的是引用的是否是同一對象,但這裏有算術運算,如果該引用爲包裝器類型則會導致自動拆箱
System.out.println(c==(a+b));
// equals 比較的是引用的對象的內容(值)是否相等,但這裏有算術運算,如果該引用爲包裝器類型則會導
 // 致自動拆箱,再自動裝箱
// a+b觸發自動拆箱得到值後,再自動裝箱與c比較
System.out.println(c.equals(a+b));
// 首先a+b觸發自動拆箱後值爲int型,所以比較的是值是否相等
System.out.println(g==(a+b));
// 首先a+b觸發自動拆箱後值爲int型,自動裝箱後爲Integer型,然後g爲Long型
System.out.println(g.equals(a+b));
// 首先a+h觸發自動拆箱後值爲long型,因爲int型的a會自動轉型爲long型的g然後自動裝箱後爲Long型,
 // 而g也爲Long型
System.out.println(g.equals(a+h));
  
}

輸出:

1
2
3
4
5
6
7
true
false
true
true
true
false
true
 這裏面需要注意的是:當 “==”運算符的兩個操作數都是包裝器類型的引用,則是比較指向的是否是同一個對象,而如果其中有一個操作數是表達式(即包含算術運算)則比較的是數值(即會觸發自動拆箱的過程)另外,對於包裝器類型,equals方法並不會進行類型轉換。
發佈了47 篇原創文章 · 獲贊 32 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章