关于Java包装类装箱拆箱的小例子
简单来说:装箱就是把值类型转变为引用类型,拆箱就是把引用类型转变为值类型
其实这东西没什么好说的,上代码看看就明白了:
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";
补充:软件开发 , Java ,