Java Objects.equals 和 Object.equals

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34486832/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 15:42:03  来源:igfitidea点击:

Objects.equals and Object.equals

javaequals

提问by jfcjohn

I try to create a tuple class that allows a tuple-like structure in Java. The general type for two elements in tuple are X and Y respectively. I try to override a correct equals for this class.

我尝试创建一个元组类,它允许在 Java 中使用类似元组的结构。元组中两个元素的一般类型分别是 X 和 Y。我尝试覆盖这个类的正确等于。

Thing is, I know Object.equals falls into default that it still compares based on references like "==", so I am not so sure I can use that. I looked into Objects and there is an equals() in it. Does this one still compare on references, or it compares on contents?

事实是,我知道 Object.equals 属于默认值,它仍然基于“==”之类的引用进行比较,所以我不太确定我可以使用它。我查看了 Objects,其中有一个 equals()。这仍然是在参考文献上进行比较,还是在内容上进行比较?

Quickly imagined the return statement as something like:

很快把 return 语句想象成这样:

return Objects.equals(compared.prev, this.prev) && Objects.equals(compared.next, this.next);

where prev and next are elements of tuple. Would this work?

其中 prev 和 next 是元组的元素。这行得通吗?

采纳答案by Bohemian

The difference is the Objects.equals()considers two nulls to be "equal". The pseudo code is:

不同之处在于将Objects.equals()两个空值视为“相等”。伪代码是:

  1. if both parameters are nullor the same object, return true
  2. if the first parameter is nullreturn false
  3. return the result of passing the second parameter to the equals()method of the first parameter
  1. 如果两个参数是null或同一个对象,则返回true
  2. 如果第一个参数是null返回false
  3. 将第二个参数传递给第一个参数的equals()方法的结果返回

This means it is "null safe" (non null safe implementation of the first parameter's equals()method notwithstanding).

这意味着它是“空安全的”(equals()尽管第一个参数的方法是非空安全的)。

回答by Natecat

Objects.equals just calls it's first arguments .equals method. In java, if you want to be able to test for equality in instances of a class you made, then you have to override the equals method. instance.equals() only uses == if that instances type doesn't override the equals method.

Objects.equals 只是调用它的第一个参数 .equals 方法。在 Java 中,如果您希望能够在您创建的类的实例中测试相等性,那么您必须覆盖 equals 方法。instance.equals() 仅在该实例类型未覆盖 equals 方法时才使用 ==。

回答by inor

The answer to your question "Does this one [Objects.equals] still compare on references, or it compares on contents?" - Objects.equals does some comparisons on the references but it expects the first argument's class to implement equals() in which the comparison of contents is done as well as on reference.

您的问题的答案“这个 [Objects.equals] 是否仍然在引用上进行比较,还是在内容上进行比较?” - Objects.equals 对引用进行一些比较,但它期望第一个参数的类实现 equals(),其中内容的比较和引用一样。

Your second question about the implementation of equals in your tupple-like class having prev and next as its tupple attributes the answer is: your suggested implementation would work only if prev and next are primitives or if their type implements equals properly. So if prev for example is of type Foo, then you can use Objects.equals to test the two Foo's only if class Foo implements equals as expected.

关于在类似元组的类中实现 equals 的第二个问题,它具有 prev 和 next 作为其元组属性,答案是:只有当 prev 和 next 是基元或者它们的类型实现正确时,您建议的实现才有效。因此,如果例如 prev 是 Foo 类型,那么您可以使用 Objects.equals 来测试两个 Foo 仅当 Foo 类按预期实现 equals 时。