java自动装箱/拆箱如何工作? [英] How java auto boxing/unboxing works?
问题描述
自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
尝试不同的情况后(短
, long
, float
, double
),唯一的情况是当编译运算符右边的值的类型是 int
时,编译器接受。
当我查看 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
),然后编译器使用转换基元类型的通常规则( byte
=> 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()
而不是构造函数(对于其他包装类也是如此)。这是有益的,因为它允许缓存,并且不会强制在每个装箱操作上创建新对象。
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()$使用c $ c>
(同样,它也类似于其他包装类型)。这实际上是所有自动(非)拳击归结为。
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.
这篇关于java自动装箱/拆箱如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!