自動裝箱和拆箱從Java 1.5開始引入,目的是將原始類型值轉自動地轉換成對應的對象。自動裝箱與拆箱的機制可讓咱們在Java的變量賦值或者是方法調用等狀況下使用原始類型或者對象類型更加簡單直接。 java
若是你在Java1.5下進行過編程的話,你必定不會陌生這一點,你不能直接地向集合(Collections)中放入原始類型值,由於集合只接收對象。一般這種狀況下你的作法是,將這些原始類型的值轉換成對象,而後將這些轉換的對象放入集合中。使用Integer,Double,Boolean等這些類咱們能夠將原始類型值轉換成對應的對象,可是從某些程度可能使得代碼不是那麼簡潔精煉。爲了讓代碼簡練,Java 1.5引入了具備在原始類型和對象類型自動轉換的裝箱和拆箱機制。可是自動裝箱和拆箱並不是完美,在使用時須要有一些注意事項,若是沒有搞明白自動裝箱和拆箱,可能會引發難以察覺的bug。 android
本文將介紹,什麼是自動裝箱和拆箱,自動裝箱和拆箱發生在何時,以及要注意的事項。 編程
自動裝箱就是Java自動將原始類型值轉換成對應的對象,好比將int的變量轉換成Integer對象,這個過程叫作裝箱,反之將Integer對象轉換成int類型值,這個過程叫作拆箱。由於這裏的裝箱和拆箱是自動進行的非人爲轉換,因此就稱做爲自動裝箱和拆箱。原始類型byte,short,char,int,long,float,double和boolean對應的封裝類爲Byte,Short,Character,Integer,Long,Float,Double,Boolean。 緩存
自動裝箱和拆箱在Java中很常見,好比咱們有一個方法,接受一個對象類型的參數,若是咱們傳遞一個原始類型值,那麼Java會自動講這個原始類型值轉換成與之對應的對象。最經典的一個場景就是當咱們向ArrayList這樣的容器中增長原始類型數據時或者是建立一個參數化的類,好比下面的ThreadLocal。 app
1
2
3
4
5
6
7
8
9
|
ArrayList<Integer> intList =newArrayList<Integer>();
intList.add(1);//autoboxing - primitive to object
intList.add(2);//autoboxing
ThreadLocal<Integer> intLocal =newThreadLocal<Integer>();
intLocal.set(4);//autoboxing
intnumber = intList.get(0);// unboxing
intlocal = intLocal.get();// unboxing in Java
|
上面的部分咱們介紹了自動裝箱和拆箱以及它們什麼時候發生,咱們知道了自動裝箱主要發生在兩種狀況,一種是賦值時,另外一種是在方法調用的時候。爲了更好地理解這兩種狀況,咱們舉例進行說明。 性能
這是最多見的一種狀況,在Java 1.5之前咱們須要手動地進行轉換才行,而如今全部的轉換都是由編譯器來完成。 優化
1
2
3
4
5
6
7
|
//before autoboxing
Integer iObject = Integer.valueOf(3);
Int iPrimitive = iObject.intValue()
//after java5
Integer iObject =3;//autobxing - primitive to wrapper conversion
intiPrimitive = iObject;//unboxing - object to primitive conversion
|
這是另外一個經常使用的狀況,當咱們在方法調用時,咱們能夠傳入原始數據值或者對象,一樣編譯器會幫咱們進行轉換。 google
1
2
3
4
5
6
7
8
|
publicstaticInteger show(Integer iParam){
System.out.println("autoboxing example - method invocation i: "+ iParam);
returniParam;
}
//autoboxing and unboxing in method invocation
show(3);//autoboxing
intresult = show(3);//unboxing because return type of method is Integer
|
show方法接受Integer對象做爲參數,當調用show(3)時,會將int值轉換成對應的Integer對象,這就是所謂的自動裝箱,show方法返回Integer對象,而int result = show(3);中result爲int類型,因此這時候發生自動拆箱操做,將show方法的返回的Integer對象轉換成int值。 spa
自動裝箱有一個問題,那就是在一個循環中進行自動裝箱操做的狀況,以下面的例子就會建立多餘的對象,影響程序的性能。 對象
1
2
3
4
|
Integer sum =0;
for(inti=1000; i<5000; i++){
sum+=i;
}
|
上面的代碼sum+=i能夠當作sum = sum + i,可是+這個操做符不適用於Integer對象,首先sum進行自動拆箱操做,進行數值相加操做,最後發生自動裝箱操做轉換成Integer對象。其內部變化以下
1
2
|
sum = sum.intValue() + i;
Integer sum =newInteger(result);
|
因爲咱們這裏聲明的sum爲Integer類型,在上面的循環中會建立將近4000個無用的Integer對象,在這樣龐大的循環中,會下降程序的性能而且加劇了垃圾回收的工做量。所以在咱們編程時,須要注意到這一點,正確地聲明變量類型,避免由於自動裝箱引發的性能問題。
當重載趕上自動裝箱時,狀況會比較有些複雜,可能會讓人產生有些困惑。在1.5以前,value(int)和value(Integer)是徹底不相同的方法,開發者不會由於傳入是int仍是Integer調用哪一個方法困惑,可是因爲自動裝箱和拆箱的引入,處理重載方法時稍微有點複雜。一個典型的例子就是ArrayList的remove方法,它有remove(index)和remove(Object)兩種重載,咱們可能會有一點小小的困惑,其實這種困惑是能夠驗證並解開的,經過下面的例子咱們能夠看到,當出現這種狀況時,不會發生自動裝箱操做。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
publicvoidtest(intnum){
System.out.println("method with primitive argument");
}
publicvoidtest(Integer num){
System.out.println("method with wrapper argument");
}
//calling overloaded method
AutoboxingTest autoTest =newAutoboxingTest();
intvalue =3;
autoTest.test(value);//no autoboxing
Integer iValue = value;
autoTest.test(iValue);//no autoboxing
Output:
method with primitive argument
method with wrapper argument
|
自動裝箱和拆箱可使代碼變得簡潔,可是其也存在一些問題和極端狀況下的問題,如下幾點須要咱們增強注意。
這是一個比較容易出錯的地方,」==「能夠用於原始值進行比較,也能夠用於對象進行比較,當用於對象與對象之間比較時,比較的不是對象表明的值,而是檢查兩個對象是不是同一對象,這個比較過程當中沒有自動裝箱發生。進行對象值比較不該該使用」==「,而應該使用對象對應的equals方法。看一個能說明問題的例子。
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
32
33
34
35
|
publicclassAutoboxingTest {
publicstaticvoidmain(String args[]) {
// Example 1: == comparison pure primitive – no autoboxing
inti1 =1;
inti2 =1;
System.out.println("i1==i2 : "+ (i1 == i2));// true
// Example 2: equality operator mixing object and primitive
Integer num1 =1;// autoboxing
intnum2 =1;
System.out.println("num1 == num2 : "+ (num1 == num2));// true
// Example 3: special case - arises due to autoboxing in Java
Integer obj1 =1;// autoboxing will call Integer.valueOf()
Integer obj2 =1;// same call to Integer.valueOf() will return same
// cached Object
System.out.println("obj1 == obj2 : "+ (obj1 == obj2));// true
// Example 4: equality operator - pure object comparison
Integer one =newInteger(1);// no autoboxing
Integer anotherOne =newInteger(1);
System.out.println("one == anotherOne : "+ (one == anotherOne));// false
}
}
Output:
i1==i2 :true
num1 == num2 :true
obj1 == obj2 :true
one == anotherOne :false
|
值得注意的是第三個小例子,這是一種極端狀況。obj1和obj2的初始化都發生了自動裝箱操做。可是處於節省內存的考慮,JVM會緩存-128到127的Integer對象。由於obj1和obj2其實是同一個對象。因此使用」==「比較返回true。
另外一個須要避免的問題就是混亂使用對象和原始數據值,一個具體的例子就是當咱們在一個原始數據值與一個對象進行比較時,若是這個對象沒有進行初始化或者爲Null,在自動拆箱過程當中obj.xxxValue,會拋出NullPointerException,以下面的代碼
1
2
3
4
5
6
|
privatestaticInteger count;
//NullPointerException on unboxing
if( count <=0){
System.out.println("Count is not started yet");
}
|
這個問題就是咱們上面提到的極端狀況,在Java中,會對-128到127的Integer對象進行緩存,當建立新的Integer對象時,若是符合這個這個範圍,而且已有存在的相同值的對象,則返回這個對象,不然建立新的Integer對象。
在Java中另外一個節省內存的例子就是字符串常量池,感興趣的同窗能夠了解一下。
由於自動裝箱會隱式地建立對象,像前面提到的那樣,若是在一個循環體中,會建立無用的中間對象,這樣會增長GC壓力,拉低程序的性能。因此在寫循環時必定要注意代碼,避免引入沒必要要的自動裝箱操做。
如想了解垃圾回收和內存優化,能夠查看本文Google IO:Android內存管理主題演講記錄