java boolean例子_Java Boolean包装器类如何实例化?

不,你不能做后者.

前者被称为autoboxing,并在Java v1.5中引入自动包装,其包装对象中的原语.

使用泛型和/或集合时,可以清楚地看到自动装箱的好处:

在“AutoBoxing and Auto-UnBoxing of Primitive Types”样本中,我们有:

之前(添加了自动装箱)

ArrayList list = new ArrayList();

list.add(0,new Integer(42));

int total = (list.get(0)).intValue();

ArrayList list = new ArrayList();

list.add(0,42);

int total = list.get(0);

如你所见,代码更清楚.

请记住文档上的最后一个注意事项:

So when should you use autoBoxing and unBoxing? Use them only when there is an “impedance mismatch” between reference types and primitives,for example,when you have to put numerical values into a collection. It is not appropriate to use autoBoxing and unBoxing for scientific computing,or other performance-sensitive numerical code. An Integer is not a substitute for an int; autoBoxing and unBoxing blur the distinction between primitive types and reference types,but they do not eliminate it.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值