Equals operator for zeros (BigDecimal / Double) in Java

后端 未结 7 1167
别那么骄傲
别那么骄傲 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条回答
  •  我在风中等你
    2020-12-14 06:52

    new Double(0.0).equals(0)
    

    This line compares a double value of 0 (which is not exact zero) with integer of 0.

    BigDecimal.ZERO.equals(BigDecimal.valueOf(0.0))
    

    BigDecimal will compare the scale length in the equals operation.

提交回复
热议问题