问题描述
自JDK 5.0起,在Java中引入了自动装箱/拆箱.这个技巧很简单而且很有帮助,但是当我开始测试包装器类和原始类型之间的不同转换时,我真的很困惑自动装箱的概念在Java中是如何工作的.例如:
Since JDK 5.0, auto boxing/unboxing was introduced in Java. The trick is simple and helpful, but when I started testing different conversions between wrapper classes and primitive types, I get really confused how the concept of auto boxing works in Java. For example:
拳击
int intValue = 0;
Integer intObject = intValue;
byte byteValue = 0;
intObject = byteValue; // ==> Error
尝试不同的情况(short
,long
,float
,double
)后,编译器唯一接受的情况是情感运算符右侧的值的类型为.当我查看Integer.class
的源代码时,发现它仅使用int
参数实现一个构造函数.
After trying different cases (short
, long
, float
, double
), the only case which is accepted by the compiler is when the type of the value on the right of affectation operator is int
.When I looked inside the source of Integer.class
I found that it implements only one constructor with int
parameter.
所以我的结论是,自动装箱的概念基于在包装器类中实现的构造函数.我想知道这个结论是否正确,还是自动装箱使用了另一个概念?
So my conclusion is that the concept of auto boxing is based on constructor implemented in the wrapper class. I want to know if this conclusion is true or there is another concept used by auto boxing?
取消装箱
Integer intObject = new Integer(0);
byte byteValue = intObject; // ==> Error (the same Error with short)
int intValue = intObject;
double doubleValue = intObject;
我对拆箱的结论是,包装器类提供了对象以相应类型(Integer
==> int
)包装的值,然后编译器使用转换原始类型( => short
=> int
=> long
=> float
=> double
).我想知道这个结论是否正确,还是自动拆箱使用了另一个概念?
My conclusion about unboxing is that the wrapper class gives the value wrapped by the object in the corresponding type (Integer
==> int
), then the compiler use the usual rules of converting primitive types (byte
=> short
=> int
=> long
=> float
=> double
).I want to know if this conclusion is true or there is another concept used by auto unboxing?
推荐答案
如有疑问,请检查字节码:
When in doubt, check the bytecode:
Integer n = 42;
成为:
0: bipush 42
2: invokestatic #16 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
5: astore_1
实际上, valueOf()
使用a>而不是使用构造函数(其他包装器类也是如此).这是有益的,因为它允许缓存,并且不会在每次装箱操作时强制创建新对象.
So in actuality, valueOf()
is used as opposed to the constructor (and the same goes for the other wrapper classes). This is beneficial since it allows for caching, and doesn't force the creation of a new object on each boxing operation.
相反的是:
int n = Integer.valueOf(42);
变为:
0: bipush 42
2: invokestatic #16 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
5: invokevirtual #22 // Method java/lang/Integer.intValue:()I
8: istore_1
即使用 intValue()
(再次,它也与其他包装类型类似).这实际上是所有自动装箱归结为.
i.e. intValue()
is used (again, it's analogous for the other wrapper types as well). This is really all auto(un)boxing boils down to.
您可以在中了解有关装箱和拆箱转换的信息. JLS§5.1.7和 JLS§ 5.1.8 .
这篇关于自动装箱/拆箱在Java中如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!