Equals operator for zeros (BigDecimal / Double) in Java

后端 未结 7 1160
别那么骄傲
别那么骄傲 2020-12-14 06:31

A few interesting observations w.r.t equals operator on 0 and 0.0

  1. new Double(0.0).equals(0) returns false, while new Double(0.0).equals(0

7条回答
  •  Happy的楠姐
    2020-12-14 06:37

    new Double(0.0).equals(0); //false
    

    as the argument you passed is integer. and the equels() in Double class checks whether the argument is od instance Double or not using instance of operator.

    The Double's equals() method.

    if (!(argument instanceof Double))
      return false;
    

    The argument you passed is integer, which is not instance of Double, so it returns false.

提交回复
热议问题