自動裝箱和拆箱從Java 1.5開始引入,目的是將原始類型值轉自動地轉換成對應的對象。自動裝箱與拆箱的機制可以讓我們在Java的變量賦值或者是方法調用等情況下使用原始類型或者對象類型更加簡單直接。
一、自動裝箱和拆箱
定義
自動裝箱就是Java自動將原始類型值轉換成對應的對象,比如將int的變量轉換成Integer對象,這個過程叫做裝箱,反之將Integer對象轉換成int類型值,這個過程叫做拆箱。因為這里的裝箱和拆箱是自動進行的非人為轉換,所以就稱作為自動裝箱和拆箱。原始類型byte,short,char,int,long,float,double和boolean對應的封裝類為Byte,Short,Character,Integer,Long,Float,Double,Boolean。要點
自動裝箱時編譯器調用valueOf將原始類型值轉換成對象,同時自動拆箱時,編譯器通過調用類似intValue(),doubleValue()這類的方法將對象轉換成原始類型值。
二、弊端
- 對象相等比較
public class BoxingTest {
public static void main(String[] args) {
Integer a = 1000;
Integer b = 1000;
Integer c = 100;
Integer d = 100;
System.out.println("a == b is " + (a == b));
System.out.println(("c == d is " + (c == d)));
}
}
輸出結果
a == b is false
c == d is true
原因分析:
Integer a = 1000賦值過程中進行了自動裝箱(autoboxing),這相當于調用 valueOf 方法。valueOf()方法如下:
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
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() {}
}
Integer a = 1000,Integer b = 1000,相當于Integer a = new Integer(1000),Integer b = new Integer(1000),所以a == b為false;
Integer c = 100,Integer b = 100,獲取IntegerCache中的同一個Integer實例。
Integer 緩存策略僅在自動裝箱(autoboxing)的時候有用,使用構造器創建的 Integer 對象不能被緩存。
- 容易混亂的對象和原始數據值
一個具體的例子就是當我們在一個原始數據值與一個對象進行比較時,如果這個對象沒有進行初始化或者為Null,在自動拆箱過程中obj.xxxValue,會拋出NullPointerException,如下面的代碼:
private static Integer count;
//NullPointerException on unboxing
if( count <= 0){
System.out.println("Count is not started yet");
}
- 生成無用對象增加GC壓力
在一個循環中進行自動裝箱操作的情況,如下面的例子就會創建多余的對象,影響程序的性能。
Integer sum = 0;
for(int i=1000; i<5000; i++){
sum+=i;
}
上面的代碼sum+=i可以看成sum = sum + i,但是+這個操作符不適用于Integer對象,首先sum進行自動拆箱操作,進行數值相加操作,最后發生自動裝箱操作轉換成Integer對象。其內部變化如下
int result = sum.intValue() + i;
Integer sum = new Integer(result);
由于我們這里聲明的sum為Integer類型,在上面的循環中會創建將近4000個無用的Integer對象,在這樣龐大的循環中,會降低程序的性能并且加重了垃圾回收的工作量。
在寫循環時一定要注意代碼,避免引入不必要的自動裝箱操作。