当对原始值和装箱值使用 == 时,是完成自动装箱,还是完成拆箱
When using == for a primitive and a boxed value, is autoboxing done, or is unboxing done
以下代码编译(Java 8):
Integer i1 = 1000;
int i2 = 1000;
boolean compared = (i1 == i2);
但是它有什么作用呢?
开箱 i1
:
boolean compared = (i1.intvalue() == i2);
或盒子i2
:
boolean compared = (i1 == new Integer(i2));
那么它是比较两个 Integer
对象(通过引用)还是两个 int
变量的值?
请注意,对于某些数字,引用比较将产生正确的结果,因为 Integer class 维护了 -128
到 127
之间的值的内部缓存(另请参阅评论TheLostMind)。这就是为什么我在示例中使用 1000
以及为什么我特别询问 unboxing/boxing 而不是比较结果的原因。
让我们做一些例子:
案例-1 :
public static void main(String[] args) {
Integer i1 = 1000;
int i2 = 1000;
boolean compared = (i1 == i2);
System.out.println(compared);
}
字节码:
....
16: if_icmpne 23 // comparing 2 integers
....
案例-2 :
public static void main(String[] args) {
Integer i1 = 1000;
Integer i2 = 1000;
//int i2 = 1000;
boolean compared = (i1 == i2);
System.out.println(compared);
}
字节码:
...
16: if_acmpne 23 // comparing references
....
因此,如果将 Integer
和 int
与 ==
进行比较,Integer
将拆箱为 int
,然后进行比较。
在比较 2 Integers
的情况下,比较 2 Integers
的引用。
定义在JLS #15.21.1:
If the operands of an equality operator are both of numeric type, or one is of numeric type and the other is convertible (§5.1.8) to numeric type, binary numeric promotion is performed on the operands (§5.6.2).
When an operator applies binary numeric promotion to a pair of operands, each of which must denote a value that is convertible to a numeric type, the following rules apply, in order:
- If any operand is of a reference type, it is subjected to unboxing conversion
[...]
所以为了回答您的问题,Integer
被拆箱为 int
。
说明
当使用==运算符自动装箱比较两个原始值时
没有发生。
当使用==运算符比较两个对象时自动装箱播放
角色.
当使用混合组合时,它包含一个对象和
原始类型和比较是使用 == 运算符拆箱完成的
发生在对象上并转换为基本类型。
请仔细阅读下面的内容link,这将帮助您通过合适的示例详细了解自动装箱。
参考 Link : http://javarevisited.blogspot.in/2012/07/auto-boxing-and-unboxing-in-java-be.html
以下代码编译(Java 8):
Integer i1 = 1000;
int i2 = 1000;
boolean compared = (i1 == i2);
但是它有什么作用呢?
开箱 i1
:
boolean compared = (i1.intvalue() == i2);
或盒子i2
:
boolean compared = (i1 == new Integer(i2));
那么它是比较两个 Integer
对象(通过引用)还是两个 int
变量的值?
请注意,对于某些数字,引用比较将产生正确的结果,因为 Integer class 维护了 -128
到 127
之间的值的内部缓存(另请参阅评论TheLostMind)。这就是为什么我在示例中使用 1000
以及为什么我特别询问 unboxing/boxing 而不是比较结果的原因。
让我们做一些例子:
案例-1 :
public static void main(String[] args) {
Integer i1 = 1000;
int i2 = 1000;
boolean compared = (i1 == i2);
System.out.println(compared);
}
字节码:
....
16: if_icmpne 23 // comparing 2 integers
....
案例-2 :
public static void main(String[] args) {
Integer i1 = 1000;
Integer i2 = 1000;
//int i2 = 1000;
boolean compared = (i1 == i2);
System.out.println(compared);
}
字节码:
...
16: if_acmpne 23 // comparing references
....
因此,如果将 Integer
和 int
与 ==
进行比较,Integer
将拆箱为 int
,然后进行比较。
在比较 2 Integers
的情况下,比较 2 Integers
的引用。
定义在JLS #15.21.1:
If the operands of an equality operator are both of numeric type, or one is of numeric type and the other is convertible (§5.1.8) to numeric type, binary numeric promotion is performed on the operands (§5.6.2).
When an operator applies binary numeric promotion to a pair of operands, each of which must denote a value that is convertible to a numeric type, the following rules apply, in order:
- If any operand is of a reference type, it is subjected to unboxing conversion [...]
所以为了回答您的问题,Integer
被拆箱为 int
。
说明
当使用==运算符自动装箱比较两个原始值时 没有发生。
当使用==运算符比较两个对象时自动装箱播放 角色.
当使用混合组合时,它包含一个对象和 原始类型和比较是使用 == 运算符拆箱完成的 发生在对象上并转换为基本类型。
请仔细阅读下面的内容link,这将帮助您通过合适的示例详细了解自动装箱。
参考 Link : http://javarevisited.blogspot.in/2012/07/auto-boxing-and-unboxing-in-java-be.html