Equals operator for zeros (BigDecimal / Double) in Java

后端 未结 7 1174
别那么骄傲
别那么骄傲 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:44

    For performance considerations BigDecimal, BigInteger caches small values 0 to 15 in case of BigDecimal (without fractions)

    BigDecimal.ZERO will be new BigDecimal(BigInteger.ZERO, 0, 0, 1) & valueOf method typically picks up from cache for 0 to 15 :)

提交回复
热议问题