Since the conversation in the comments has progressed due to your different questions, I will try to give you a general answer here.
I do not know why this @override
:
@override
This is because the equals
method is a method of the language itself and you are telling the IDE
that you are overwriting the method to perform your specific task within that method.
For more information regarding @override
:
I do not understand this:
Punto other = (Punto) obj;
That obj
that the method receives per parameter is converting it into a type Punto
, and then get x
& y
.
if (this == obj)
does this mean that if all its attributes are the same as the object that arrives by parameter?
No, it does not mean that the attributes are the same, if (this == obj)
here ==
compares object references, checks whether the two operands point to the same object (not equivalent objects, the same object >).
See this for more information:
Why do you transform the number with doubleToLongBits
?:
Short answer: Eclipse uses Double.doubleToLongBits because that's what it does Double.equals :
The result is true
if and only if the argument is not null
and is a double
object that represents a double
that has the same value as the double
represented by this object. For this purpose, two values double
are considered equal if and only if the method doubleToLongBits(double)
returns the same value long
when applied to each one.
Long answer: JLS specifies some differences between Double.equals
and ==
. For a difference specified in JLS 4.2.3 and JLS 15.21.1 :
The positive zero and the negative zero are compared equal; therefore, the result of the expression 0.0==-0.0
is true
and the result of 0.0>-0.0
is false
. But other operations can distinguish positive and negative zero; for example, 1.0/0.0
has the positive infinite value, while the value of 1.0/-0.0
is infinite negative.
Another respect NaN
:
If any of the operands is NaN
, then the result of ==
is false
but the result of !=
is true
.
In fact, the test x!=x
is true
if and only if the value of x
is NaN
.
As you can see, it is possible to compare two double values, ==
but in reality correspond to different behaviors when used in mathematical tables and hash
. Therefore, when writing a generated equality method, Eclipse assumes that two doubles are only equal if and only if all the operations that can be done are identical, or (equivalently) if they auto-box and compare with their methods equals
. This is particularly important if you change between double
and Double
, it would be particularly unexpected that the equality properties differ there.
Of course, you are free to deviate from that assumption: regardless of whether it is a good idea, you can assign special cases to any of the many possible representations of NaN
, in which case Double.doubleToRawLongBits()
would be a better match for your equals
.
OS Source: Meaning of Double.doubleToLongBits (x)