I have two double values (primitive types and NOT objects!) and when I let Eclipse generate equals/hashCode
for these double values, then it will override it like that:
@Override
public boolean equals(Object obj) {
...
return Double.doubleToLongBits(mydouble) == Double.doubleToLongBits(obj.mydouble) && ..;
}
So it uses doubleToLongBits
to compare for equality. However, when using doubleToLongBits
the comparision of 0!=-0
returns false. Why is this wanted within equals
-method?
My question: Why Eclipse does not simply use the
@Override
public boolean equals(Object obj) {
..
return mydouble == obj.mydouble && ..;
}
to compare for equality? Would this be wrong?
Problem is specific for primitive types and why one should favor doubleToLongBits
with primitive types instead of simply use, e.g. 0.1 == 0.1
.