简单来说:装箱就是把值类型转变为引用类型,拆箱就是把引用类型转变为值类型
基本数据类型的自动装箱(autoboxing)、拆箱(unboxing)是自J2SE 5.0开始提供的功能。
一般我们要创建一个类的对象的时候,我们会这样: Class a = new Class(parameter); 当我们创建一个Integer对象时,却可以这样: Integer i = 100; (注意:不是 int i = 100; ) 实际上,执行上面那句代码的时候,系统为我们执行了:Integer i = new Integer(100); 此即基本数据类型的自动装箱功能。
示例代码:
1 /** 2 * @author hellosure 3 * @time 2011-7-27 上午8:10:46 4 * @description:装箱拆箱例子 5 */ 6 public class Test { 7 8 public static void main(String arg[]) { 9 int v1 = 100; 10 int v2 = 100; 11 //自动装箱 12 Integer autovalue1 = 100 ; 13 Integer autovalue2 = 100 ; 14 //手动装箱两种方式 15 Integer value1 = Integer.valueOf(v1); 16 Integer value2 = Integer.valueOf(v2); 17 Integer va1 = new Integer(v1); 18 Integer va2 = new Integer(v2); 19 //自动拆箱 20 int autov1 = autovalue1; 21 int autov2 = autovalue2; 22 //手动拆箱 23 int vv1 = value1.intValue(); 24 int vv2 = value2.intValue(); 25 26 27 System.out.println(" v1 == v2 is "+ (v1 == v2)); 28 System.out.println(" autovalue1 == autovalue2 is "+ (autovalue1 == autovalue2)); 29 System.out.println(" value1 == value2 is " + (value1 == value2)); 30 System.out.println(" va1 == va2 is "+ (va1 == va2)); 31 System.out.println(" va1 equals va2 is "+ (va1.equals(va2))); 32 System.out.println(" autov1 == autov2 is "+ (autov1 == autov2)); 33 System.out.println(" vv1 == vv2 is "+ (vv1 == vv2)); 34 35 System.out.println("-----------------------------------------------------"); 36 37 String strv1 = "100"; 38 String strv2 = "100"; 39 String stringv1 = new String("100"); 40 String stringv2 = new String("100"); 41 Integer strvalue1 = Integer.parseInt(strv1); 42 Integer strvalue2 = Integer.parseInt(strv2); 43 Integer stringvalue1 = Integer.parseInt(stringv1); 44 Integer stringvalue2 = Integer.parseInt(stringv2); 45 Integer newstrv1 = new Integer(strv1); 46 Integer newstrv2 = new Integer(strv2); 47 48 System.out.println(" strv1 == strv2 is "+ (strv1 == strv2)); 49 System.out.println(" stringv1 == stringv2 is "+ (stringv1 == stringv2)); 50 System.out.println(" stringv1 equals stringv2 is "+ (stringv1.equals(stringv2))); 51 System.out.println(" strvalue1 == strvalue2 is "+ (strvalue1 == strvalue2)); 52 System.out.println(" stringvalue1 == stringvalue2 is "+ (stringvalue1 == stringvalue2)); 53 System.out.println(" newstrv1 == newstrv2 is "+ (newstrv1 == newstrv2)); 54 System.out.println(" newstrv1 equals newstrv2 is "+ (newstrv1.equals(newstrv2))); 55 56 System.out.println("-----------------------------------------------------"); 57 58 int v3 = 200; 59 int v4 = 200; 60 //自动装箱 61 Integer autovalue3 = 200 ; 62 Integer autovalue4 = 200 ; 63 //手动装箱两种方式 64 Integer value3 = Integer.valueOf(v3); 65 Integer value4 = Integer.valueOf(v4); 66 Integer va3 = new Integer(v3); 67 Integer va4 = new Integer(v4); 68 //自动拆箱 69 int autov3 = autovalue3; 70 int autov4 = autovalue4; 71 //手动拆箱 72 int vv3 = value3.intValue(); 73 int vv4 = value4.intValue(); 74 75 76 System.out.println(" v3 == v4 is "+ (v3 == v4)); 77 System.out.println(" autovalue3 == autovalue4 is "+ (autovalue3 == autovalue4)); 78 System.out.println(" value3 == value4 is " + (value3 == value4)); 79 System.out.println(" va3 == va4 is "+ (va3 == va4)); 80 System.out.println(" va3 equals va4 is "+ (va3.equals(va4))); 81 System.out.println(" autov3 == autov4 is "+ (autov3 == autov4)); 82 System.out.println(" vv3 == vv4 is "+ (vv3 == vv4)); 83 84 System.out.println("-----------------------------------------------------"); 85 86 String strv3 = "200"; 87 String strv4 = "200"; 88 String stringv3 = new String("200"); 89 String stringv4 = new String("200"); 90 Integer strvalue3 = Integer.parseInt(strv3); 91 Integer strvalue4 = Integer.parseInt(strv4); 92 Integer stringvalue3 = Integer.parseInt(stringv3); 93 Integer stringvalue4 = Integer.parseInt(stringv4); 94 Integer newstrv3 = new Integer(strv3); 95 Integer newstrv4 = new Integer(strv4); 96 97 System.out.println(" strv3 == strv4 is "+ (strv3 == strv4)); 98 System.out.println(" stringv3 == stringv4 is "+ (stringv3 == stringv4)); 99 System.out.println(" stringv3 equals stringv4 is "+ (stringv3.equals(stringv4))); 100 System.out.println(" strvalue3 == strvalue4 is "+ (strvalue3 == strvalue4)); 101 System.out.println(" stringvalue3 == stringvalue4 is "+ (stringvalue3 == stringvalue4)); 102 System.out.println(" newstrv3 == newstrv4 is "+ (newstrv3 == newstrv4)); 103 System.out.println(" newstrv3 equals newstrv4 is "+ (newstrv3.equals(newstrv4))); 104 105 System.out.println("-----------------------------------------------------"); 106 107 } 108 }
运行结果:
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 ----------------------------------------------------
说明:
equals() 比较的是两个对象的值(内容)是否相同。
"==" 比较的是两个对象的引用(内存地址)是否相同,也用来比较两个基本数据类型的变量值是否相等,对于new创建出的两个对象,用==比较肯定是不同的,因为指向的不是同一内存。
输出的结果是:
在自动装箱时对于值从–128到127之间的值,它们被装箱为Integer对象后,会存在内存中被重用,
所以范例中,i3 与 i4实际上参考至同一个对象。
如果超过了从–128到127之间的值,被装箱后的Integer对象并不会被重用,
即相当于每次装箱时都新建一个 Integer对象,所以范例中,i1与i2参考的是不同的对象。
Integer装箱过程中调用的是valueOf方法,而 valueOf方法对值在-128到127之间的数值缓存了,源代码如下:
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对象。