Java 中是否保证 new Integer(i) == i ?
考虑以下代码片段:
int i = 99999999;
byte b = 99;
short s = 9999;
Integer ii = Integer.valueOf(9); // should be within cache
System.out.println(new Integer(i) == i); // "true"
System.out.println(new Integer(b) == b); // "true"
System.out.println(new Integer(s) == s); // "true"
System.out.println(new Integer(ii) == ii); // "false"
很明显为什么最后一行将 ALWAYS 打印 "false"
:我们使用 ==
引用身份比较,并且新
对象将永远==
到已经存在的对象。
问题是关于前 3 行:这些比较是否保证在原始 int
上,与 Integer
自动拆箱?是否存在原语被自动装箱并执行参考身份比较的情况? (这一切都将是false
!)
Consider the following snippet:
int i = 99999999;
byte b = 99;
short s = 9999;
Integer ii = Integer.valueOf(9); // should be within cache
System.out.println(new Integer(i) == i); // "true"
System.out.println(new Integer(b) == b); // "true"
System.out.println(new Integer(s) == s); // "true"
System.out.println(new Integer(ii) == ii); // "false"
It's obvious why the last line will ALWAYS prints "false"
: we're using ==
reference identity comparison, and a new
object will NEVER be ==
to an already existing object.
The question is about the first 3 lines: are those comparisons guaranteed to be on the primitive int
, with the Integer
auto-unboxed? Are there cases where the primitive would be auto-boxed instead, and reference identity comparisons are performed? (which would all then be false
!)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的。 JLS §5.6.2 指定二进制数字提升的规则。部分:
二进制数字提升适用于多个数字运算符,包括“数字相等运算符 == 和 !=”。
JLS §15.21.1(数字相等运算符 == 和 !=) 指定:
相比之下,JLS §15.21.3(参考相等运算符 == 和 !=)提供:
这符合装箱和拆箱的一般理解,只有在不匹配时才会执行。
Yes. JLS §5.6.2 specifies the rules for binary numeric promotion. In part:
Binary numeric promotion applies for several numeric operators, including "the numerical equality operators == and !=."
JLS §15.21.1 (Numerical Equality Operators == and !=) specifies:
In contrast, JLS §15.21.3 (Reference Equality Operators == and !=) provides:
This fits the common understanding of boxing and unboxing, that's it only done when there's a mismatch.
我将首先准确地解释何时
==
是引用相等,以及准确地何时 它是数值相等。引用相等的条件比较简单,所以先解释一下。JLS 15.21.3 引用相等运算符
= =
和!=
这解释了以下内容:
两个操作数都是
Integer
,它们是引用类型,这就是为什么==
是引用相等比较,并且两个new
对象之间永远不会==
,所以这就是它打印false
的原因。要使
==
数值相等,至少有一个操作数必须是数值类型;指定如下:JLS 15.21.1 数值相等运算符
==
和!=
因此,请考虑以下情况:
这将打印
true
,因为:int
类型int
==
是一个数字相等运算摘要
==
和==
的两个操作数code>!= 是引用类型,它始终是引用相等操作参考文献
==
和!=
==
和!=
相关问题
整数
时,是否会发生自动拆箱?==
而不是equals()
?I will first explain precisely when
==
is a reference equality, and precisely when it's a numerical equality. The conditions for reference equality is simpler, so it will be explained first.JLS 15.21.3 Reference Equality Operators
==
and!=
This explains the following:
Both operands are
Integer
, which are reference types, and that's why the==
is reference equality comparison, and twonew
objects will never be==
to each other, so that's why it printsfalse
.For
==
to be numerical equality, at least one of the operand must be a numeric type; this is specified as follows:JLS 15.21.1 Numerical Equality Operators
==
and!=
Thus, consider the following:
This prints
true
, because:int
typeint
==
is a numerical equality operationSummary
==
and!=
are reference types, it will always be a reference equality operationReferences
==
and!=
==
and!=
Related questions
Integers
in Java does auto-unboxing occur?==
but notequals()
?