为什么这些==但不是`equals()`? [英] Why are these == but not `equals()`?

查看:170
本文介绍了为什么这些==但不是`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]




  1. X == Y 不能编译,是不同的对象。

  2. 我有点惊讶 Y == 9 是真的鉴于9默认为 int ,并且假设1)甚至没有编译。请注意,您不能将 int 放入期望 Short 的方法中,但在这里它们是相等的。

  3. 出于与2相同的原因,这是令人惊讶的,但看起来更糟。

  4. 不足为奇,因为 x 自动装箱到和整数

  5. 不足为奇,因为不同类中的对象不应该是等于()

  6. 什么? X == y true 但是 X.equals(y) false ?不应该 == 总是比更严格等于()

  1. That X == Y does not compile is to be expected, being different objects.
  2. I'm a little surprised that Y == 9 is true, given that 9 is by default an int, and given that 1) didn't even compile. Note that you can't put an int into a method expecting a Short, yet here they are equal.
  3. This is surprising for the same reason as two, but it seems worse.
  4. Not surprising, as x is autoboxed to and Integer.
  5. Not surprising, as objects in different classes should not be equal().
  6. What?? X == y is true but X.equals(y) is false? Shouldn't == always be stricter than equals()?

如果有人能帮助我理解这一点,我会很感激。出于什么原因,==和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:


  1. 不同的实例,如此不同。

  2. X 未装箱,然后是相同的值,所以相等。

  3. 相同的值,如此相等。

  4. y 无法装箱到整数所以不能相等。

  1. Different instance, so different.
  2. X unboxed, then same value, so equal.
  3. Same value, so equal.
  4. y cannot be boxed to an Integer 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆