Comparison with Equals

0

This code works well but I can not understand its operation, I did not know how to ask the question, because I do not have to solve anything just understand it. The code is taken from a class Point that has two attributes (x, y).

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;//
    if (obj == null)
        return false;
    if (!(obj instanceof Punto))
        return false;
    Punto other = (Punto) obj;//aqui ya no entiendo nada
    if (Double.doubleToLongBits(x) != Double.doubleToLongBits(other.x))
        return false;
    if (Double.doubleToLongBits(y) != Double.doubleToLongBits(other.y))
        return false;
    return true;
}
    
asked by David Palanco 14.05.2018 в 20:38
source

1 answer

0

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)

    
answered by 14.05.2018 / 22:12
source