Java學習之自動裝箱和自動拆箱源碼分析
自動裝箱(boxing)和自動拆箱(unboxing)
首先了解下Java的四類八種基本數據類型
| 基本類型 | 占用空間(Byte) | 表示范圍 | 包裝器類型 |
|
boolean |
1/8 |
true|false |
Boolean |
|
char |
2 |
-128~127 |
Character |
|
byte |
1 |
-128~127 |
Byte |
|
short |
2 |
-2?15~2?15-1 |
Short |
|
int |
4 |
-2?31~2?31-1 |
Integer |
|
long |
8 |
-2?63~2?63-1 |
Long |
|
float |
4 |
-3.403E38~3.403E38 |
Float |
|
double |
8 |
-1.798E308~1.798E308 |
Double |
自動裝箱
Java中所謂的裝箱通俗點就是:八種基本數據類型在某些條件下使用時,會自動變為對應的包裝器類型。
如下清單1:
@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);
} 輸出:
true false
解釋下清單1第11句輸出true的原因:
當包裝器類型進行“==”比較時,i3會調用Integer.valueOf自動裝箱基本數據類型為包裝器類型。
/**
* 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:
- Integer、Short、Byte、Character、Long這幾個包裝類的valueOf方法的實現是類似的;
- Double、Float的valueOf方法的實現是類似的。
- Boolean的valueOf方法的實現是個三目運算,形如` return (b ? TRUE : FALSE); `
自動拆箱
Java中所謂的拆箱通俗點就是:八種包裝器類型在某些條件下使用時,會自動變為對應的基本數據類型。
清單2:
@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);
} 輸出:
true true
解釋下清單2第10句輸出true的原因:
當程序執行到第10句時,i4會調用Integer.intValue方法自動拆箱包裝器類型為基本數據類型。
/**
* Returns the value of this {@code Integer} as an
* {@code int}.
*/
public int intValue() {
return value;
} 從源碼可以看出,當包裝器類型和基本數據類型進行“==”比較時,包裝器類型會自動拆箱為基本數據類型。
清單3內容如下:
@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));
} 輸出:
true false true true
解釋第15句為什么會輸出true:
因為在Integer包裝類實現的equals方法中,只要比較的當前對象是Integer實例,那么就會自動拆箱為基本數據類型。從以下Integer類的equals方法的源碼就可看出:
/**
* 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:
- Integer、Short、Byte、Character、Long這幾個包裝類的intValue方法的實現是類似的;
- Double、Float的intValue方法的實現是類似的。
- Boolean的booleanValue方法的實現和intValue方法的實現也是類似的。
裝箱拆箱綜合清單:
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));
} 輸出:
true false true true true false true
這里面需要注意的是:當 “==”運算符的兩個操作數都是包裝器類型的引用,則是比較指向的是否是同一個對象,而如果其中有一個操作數是表達式(即包含算術運算)則比較的是數值(即會觸發自動拆箱的過程)另外,對于包裝器類型,equals方法并不會進行類型轉換。
原文出處: 等風的草
本文由用戶 byc7 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!