Java signed zero and boxing

It is all explained in the javadoc:

Note that in most cases, for two instances of class Double, d1 and d2, the value of d1.equals(d2) is true if and only if

   d1.doubleValue() == d2.doubleValue() 

also has the value true. However, there are two exceptions:

  • If d1 and d2 both represent Double.NaN, then the equals method returns true, even though Double.NaN==Double.NaN has the value false.
  • If d1 represents +0.0 while d2 represents -0.0, or vice versa, the equal test has the value false, even though +0.0==-0.0 has the value true.

This definition allows hash tables to operate properly.


Now you might ask why 0.0 == -0.0 is true. In fact they are not strictly identical. For example:

Double.doubleToRawLongBits(0.0) == Double.doubleToRawLongBits(-0.0); //false

is false. However, the JLS requires (“in accordance with the rules of the IEEE 754 standard“) that:

Positive zero and negative zero are considered equal.

hence 0.0 == -0.0 is true.

Leave a Comment

deneme bonusu veren sitelerbahis casinomakrobetceltabetpinbahispolobetpolobet girişpinbahis girişmakrobet girişpulibet girişmobilbahis girişkolaybet giriş