关于Java包装类装箱拆箱的小例子

简单来说:[b]装箱[/b]就是把值类型转变为引用类型,[b]拆箱[/b]就是把引用类型转变为值类型
其实这东西没什么好说的,上代码看看就明白了:

/**
* @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("-----------------------------------------------------");

}
}

运行结果:

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
-----------------------------------------------------

小结:
[list]
[*]对于new创建出的两个对象,用==比较肯定是不同的,因为指向的不是同一内存
[*]Integer装箱过程中调用的是valueOf方法,而 valueOf方法对值在-128到127之间的数值缓存了,源代码如下:
[/list]

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对象。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值