Is there a difference between BigDecimal(“0”) and BigDecimal.ZERO?

后端 未结 4 1423
忘了有多久
忘了有多久 2020-12-30 21:56

Either for comparisons or initialization of a new variable, does it make a difference which one of these you use?

I know that BigDecimal.ZERO is a 1.5 feature, so th

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-30 22:33

    Before talking about runtime penalties, make sure that this piece of code matters. Set up profiling and measure the complete use case.

    Nevertheless, prefer Bigdecimal.ZERO as it's checked at compile time whereas you can accidentally type new BigDecimal("9"), which the compiler will accept, but which will cause bugs into your application.

提交回复
热议问题