簡單來說:裝箱就是把值類型轉變為引用類型,拆箱就是把引用類型轉變為值類型
其實這東西沒什麼好說的,上代碼看看就明白瞭:
Java代碼
/**
* @author hellosure
* @time 2011-7-27 上午8:10:46
* @description:裝箱拆箱例子
*/
public class Test {
public static void main(String arg[]) {
int v1 = 100;
int v2 = 100;
//自動裝箱
Integer autovalue1 = 100 ;
Integer autovalue2 = 100 ;
//手動裝箱兩種方式
Integer value1 = Integer.valueOf(v1);
Integer value2 = Integer.valueOf(v2);
Integer va1 = new Integer(v1);
Integer va2 = new Integer(v2);
//自動拆箱
int autov1 = autovalue1;
int autov2 = autovalue2;
//手動拆箱
int vv1 = value1.intValue();
int vv2 = value2.intValue();
System.out.println(" v1 == v2 is "+ (v1 == v2));
System.out.println(" autovalue1 == autovalue2 is "+ (autovalue1 == autovalue2));
System.out.println(" value1 == value2 is " + (value1 == value2));
System.out.println(" va1 == va2 is "+ (va1 == va2));
System.out.println(" va1 equals va2 is "+ (va1.equals(va2)));
System.out.println(" autov1 == autov2 is "+ (autov1 == autov2));
System.out.println(" vv1 == vv2 is "+ (vv1 == vv2));
System.out.println("—————————————————–");
String strv1 = "100";
String strv2 = "100";
String stringv1 = new String("100");
String stringv2 = new String("100");
Integer strvalue1 = Integer.parseInt(strv1);
Integer strvalue2 = Integer.parseInt(strv2);
Integer stringvalue1 = Integer.parseInt(stringv1);
Integer stringvalue2 = Integer.parseInt(stringv2);
Integer newstrv1 = new Integer(strv1);
Integer newstrv2 = new Integer(strv2);
System.out.println(" strv1 == strv2 is "+ (strv1 == strv2));
System.out.println(" stringv1 == stringv2 is "+ (stringv1 == stringv2));
System.out.println(" stringv1 equals stringv2 is "+ (stringv1.equals(stringv2)));
System.out.println(" strvalue1 == strvalue2 is "+ (strvalue1 == strvalue2));
System.out.println(" stringvalue1 == stringvalue2 is "+ (stringvalue1 == stringvalue2));
System.out.println(" newstrv1 == newstrv2 is "+ (newstrv1 == newstrv2));
System.out.println(" newstrv1 equals newstrv2 is "+ (newstrv1.equals(newstrv2)));
System.out.println("—————————————————–");
int v3 = 200;
int v4 = 200;
//自動裝箱
Integer autovalue3 = 200 ;
Integer autovalue4 = 200 ;
//手動裝箱兩種方式
Integer value3 = Integer.valueOf(v3);
Integer value4 = Integer.valueOf(v4);
Integer va3 = new Integer(v3);
Integer va4 = new Integer(v4);
//自動拆箱
int autov3 = autovalue3;
int autov4 = autovalue4;
//手動拆箱
int vv3 = value3.intValue();
int vv4 = value4.intValue();
System.out.println(" v3 == v4 is "+ (v3 == v4));
System.out.println(" autovalue3 == autovalue4 is "+ (autovalue3 == autovalue4));
System.out.println(" value3 == value4 is " + (value3 == value4));
System.out.println(" va3 == va4 is "+ (va3 == va4));
System.out.println(" va3 equals va4 is "+ (va3.equals(va4)));
System.out.println(" autov3 == autov4 is "+ (autov3 == autov4));
System.out.println(" vv3 == vv4 is "+ (vv3 == vv4));
System.out.println("—————————————————–");
String strv3 = "200";
String strv4 = "200";
String stringv3 = new String("200");
String stringv4 = new String("200");
Integer strvalue3 = Integer.parseInt(strv3);
Integer strvalue4 = Integer.parseInt(strv4);
Integer stringvalue3 = Integer.parseInt(stringv3);
Integer stringvalue4 = Integer.parseInt(stringv4);
Integer newstrv3 = new Integer(strv3);
Integer newstrv4 = new Integer(strv4);
System.out.println(" strv3 == strv4 is "+ (strv3 == strv4));
System.out.println(" stringv3 == stringv4 is "+ (stringv3 == stringv4));
System.out.println(" stringv3 equals stringv4 is "+ (stringv3.equals(stringv4)));
System.out.println(" strvalue3 == strvalue4 is "+ (strvalue3 == strvalue4));
System.out.println(" stringvalue3 == stringvalue4 is "+ (stringvalue3 == stringvalue4));
System.out.println(" newstrv3 == newstrv4 is "+ (newstrv3 == newstrv4));
System.out.println(" newstrv3 equals newstrv4 is "+ (newstrv3.equals(newstrv4)));
System.out.println("—————————————————–");
}
}
運行結果:
Java代碼
v1 == v2 is true
autovalue1 == autovalue2 is true
value1 == value2 is true
va1 == va2 is false
va1 equals va2 is true
autov1 == autov2 is true
vv1 == vv2 is true
—————————————————-
strv1 == strv2 is true
stringv1 == stringv2 is false
stringv1 equals stringv2 is true
strvalue1 == strvalue2 is true
stringvalue1 == stringvalue2 is true
newstrv1 == newstrv2 is false
newstrv1 equals newstrv2 is true
—————————————————-
v3 == v4 is true
autovalue3 == autovalue4 is false
value3 == value4 is false
va3 == va4 is false
va3 equals va4 is true
autov3 == autov4 is true
vv3 == vv4 is true
—————————————————-
strv3 == strv4 is true
stringv3 == stringv4 is false
stringv3 equals stringv4 is true
strvalue3 == strvalue4 is false
stringvalue3 == stringvalue4 is false
newstrv3 == newstrv4 is false
newstrv3 equals newstrv4 is true
—————————————————-
小結:
對於new創建出的兩個對象,用==比較肯定是不同的,因為指向的不是同一內存
Integer裝箱過程中調用的是valueOf方法,而 valueOf方法對值在-128到127之間的數值緩存瞭,源代碼如下:
Java代碼
public static Integer valueOf(int i) {
if(i >= -128 && i <= IntegerCache.high)
return IntegerCache.cache[i + 128];
else
return new Integer(i);
}
可見,Integer緩存中有一個靜態的Integer數組,在類加載時就將-128 到 127 的Integer對象創建瞭,並保存在cache數組中,一旦程序調用valueOf 方法,如果i的值是在-128 到 127 之間就直接在cache緩存數組中去取Integer對象。