为什么这些==但不是`equals()`? [英] Why are these == but not `equals()`?
问题描述
我对Java处理 ==
和 equals()
的方式感到有点困惑 int
,整数
和其他类型的数字。例如:
I'm a bit confused about the way Java treats ==
and equals()
when it comes to int
, Integer
and other types of numbers. For example:
Integer X = 9000;
int x = 9000;
Short Y = 9000;
short y = 9000;
List<Boolean> results = new ArrayList<Boolean>();
// results.add(X == Y); DOES NOT COMPILE 1)
results.add(Y == 9000); // 2)
results.add(X == y); // 3)
results.add(X.equals(x)); // 4)
results.add(X.equals(Y)); // 5)
results.add(X.equals(y)); // 6)
System.out.println(results);
输出(也许你应该先猜测):
outputs (maybe you should make your guess first):
[true, true, true, false, false]
-
X == Y
不能编译,是不同的对象。 - 我有点惊讶
Y == 9
是真的
鉴于9默认为int
,并且假设1)甚至没有编译。请注意,您不能将int
放入期望Short
的方法中,但在这里它们是相等的。 - 出于与2相同的原因,这是令人惊讶的,但看起来更糟。
- 不足为奇,因为
x
自动装箱到和整数
。 - 不足为奇,因为不同类中的对象不应该是
等于()
。 - 什么?
X == y
true
但是X.equals(y)
是false
?不应该==
总是比更严格等于()
?
- That
X == Y
does not compile is to be expected, being different objects. - I'm a little surprised that
Y == 9
istrue
, given that 9 is by default anint
, and given that 1) didn't even compile. Note that you can't put anint
into a method expecting aShort
, yet here they are equal. - This is surprising for the same reason as two, but it seems worse.
- Not surprising, as
x
is autoboxed to andInteger
. - Not surprising, as objects in different classes should not be
equal()
. - What??
X == y
istrue
butX.equals(y)
isfalse
? Shouldn't==
always be stricter thanequals()
?
如果有人能帮助我理解这一点,我会很感激。出于什么原因,==和equals()会以这种方式运行?
I'd appreciate it if anyone can help me make sense of this. For what reason do == and equals() behave this way?
编辑:我已将9更改为9000以显示此行为是与-128到127之间的整数表现不同的任何异常方式无关。
I have changed 9 to 9000 to show that this behavior is not related to the any unusual ways that the integers from -128 to 127 behave.
2 nd 编辑:好的,如果你认为你理解这些东西,你应该考虑以下,只是为了确保:
2nd OK, if you think you understand this stuff, you should consider the following, just to make sure:
Integer X = 9000;
Integer Z = 9000;
short y = 9000;
List<Boolean> results = new ArrayList<Boolean>();
results.add(X == Z); // 1)
results.add(X == y); // 2)
results.add(X.equals(Z)); // 3)
results.add(X.equals(y)); // 4)
System.out.println(results);
输出:
[false, true, true, false]
原因,就像我一样理解它:
The reason, as best as I understand it:
- 不同的实例,如此不同。
-
X
未装箱,然后是相同的值,所以相等。 - 相同的值,如此相等。
-
y
无法装箱到整数
所以不能相等。
- Different instance, so different.
X
unboxed, then same value, so equal.- Same value, so equal.
y
cannot be boxed to anInteger
so cannot be equal.
推荐答案
原因
X == y
为真与二进制数字提升。当等于运算符的至少一个操作数可转换为数字类型时,数字相等运算符。首先,第一个操作数是未装箱的。然后,两个操作数都转换为 int
。
being true has to do with binary numeric promotion. When at least one operand to the equality operator is convertible to a numeric type, the numeric equality operator is used. First, the first operand is unboxed. Then, both operands are converted to int
.
虽然
X.equals(y)
是正常的功能调用。如前所述, y
将自动装箱到 Short
对象。 Integer.equals
如果参数不是 Integer
实例,则始终返回false。通过检查实现可以很容易地看出这一点。
is a normal function call. As has been mentioned, y
will be autoboxed to a Short
object. Integer.equals
always returns false if the argument is not an Integer
instance. This can be easily seen by inspecting the implementation.
有人可能会认为这是一个设计缺陷。
One could argue that this is a design flaw.
这篇关于为什么这些==但不是`equals()`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!