比较 2 点 - java

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19710455/
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-12 19:35:05  来源:igfitidea点击:

Comparing 2 Points - java

java

提问by Aaron Priesterroth

I've noticed that you can't compare 2 points like this:

我注意到你不能像这样比较两点:

if (pointOne == pointTwo) { }

I always have to do it like this:

我总是必须这样做:

if (pointOne.x == pointTwo.x && pointOne.y == pointTwo.y) { }

I really wonder why you can not use the first example, does anyone have an answer?

我真的想知道为什么你不能使用第一个例子,有人有答案吗?

采纳答案by neutrino

You must use the equalsmethod of class Point. Check this.

您必须使用equalsclass的方法Point。检查这个

If you use == what you are actually doing is checking if the memory address of the two Pointobjects is the same.

如果你使用 == 你实际上在做的是检查两个Point对象的内存地址是否相同。

In Java, all classes are derived from Object, and you can override the equalsmethod, providing a convenient way of checking if in fact, two objects of the same Objectderived class, are the same.

在 Java 中,所有类都派生自Object,您可以覆盖该equals方法,从而提供一种方便的方法来检查同一Object派生类的两个对象实际上是否相同。

回答by Sachin Kainth

Well this will be an object comparison. You would be comparing memory locations if you compare using ==.

那么这将是一个对象比较。如果您使用 == 进行比较,您将比较内存位置。

You could override and call Equals.

您可以覆盖并调用 Equals。

回答by Subhrajyoti Majumder

==operator checks both reference pointing to the same object or not.

==运算符检查两个引用是否指向同一个对象。

You should use equalsmethod of Pointobject -

你应该使用对象的equals方法Point-

pointOne.equals(pointTwo);

Determines whether or not two points are equal. Two instances of Point2D are equal if the values of their x and y member fields, representing their position in the coordinate space, are the same.

判断两点是否相等。如果表示它们在坐标空间中的位置的 x 和 y 成员字段的值相同,则 Point2D 的两个实例相等。

Documentation

文档

回答by Diego Catalano

Because the Point is reference, and if you need to use equals, you need to override the method equal. Java doest not support override operators like "==".

因为Point是引用,如果需要使用equals,就需要重写equal方法。Java 不支持像“==”这样的覆盖操作符。

回答by Blady

I assume pointOneand pointTwoare objects of some class? You cannot overload operators in java, that is why you have to compare fields. In such cases it is a good practive to override the equals method for your class and use it this way:

我假设pointOne并且pointTwo是某个类的对象?您不能在 Java 中重载运算符,这就是您必须比较字段的原因。在这种情况下,重写类的 equals 方法并以这种方式使用它是一个很好的做法:

PointOne.equals(PointTwo)

回答by k2col

You can use your first example because running pointOne.x == pointTwo.xcompares the primitive types (int) which the == operator is capable of.

您可以使用第一个示例,因为运行pointOne.x == pointTwo.x比较 == 运算符能够执行 的基本类型 (int)。

When you run pointOne == pointTwoyou're comparing the Point object references, which they're not.

当您运行时,pointOne == pointTwo您正在比较 Point 对象引用,而它们不是。

As others have said, you can use pointOne.equals(pointTwo)

正如其他人所说,您可以使用 pointOne.equals(pointTwo)