如果包装器使用拆箱,那么intValue()方法的需要是什么? [英] What is the need of an intValue() method if wrappers use unboxing?

查看:111
本文介绍了如果包装器使用拆箱,那么intValue()方法的需要是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,查看以下代码:

Integer myInt = new Integer(5);
int i1 = myInt.intValue();
int i2 = myInt;

System.out.println(i1);
System.out.println(i2);

如您所见,我有两种方法可以将包装器中的整数值复制到原始值:

As you can see, I have two ways of copying my integer value from the wrapper to the primive:

我可以使用取消装箱

我可以使用方法intValue()

I can use the method intValue()

那么......当已经取消装箱时需要一个方法?

So... what's the need of having a method when there is already unboxing?

推荐答案

Unboxing是在Java 5中引入的。自原始版本发布以来,包装器(包括此方法)一直存在。

Unboxing was introduced in Java 5. The wrappers (including this method) have been there since the original release.

Javadoc的链接

在那段时间(1996年),我们确实需要 intValue()方法,并且作为Oracle保证向后向后兼容性...达到一定水平(主要版本并不总是100%)。

In that time (1996) we did need the intValue() method and as Oracle guarantees backward backwards compatibility... up to a certain level (it is not always 100% on major releases).

该方法必须保持。

这篇关于如果包装器使用拆箱,那么intValue()方法的需要是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆