BigDecimal.ZERO vs. new BigDecimal(0). Which to use and why?

Mathematically, they’re the same. Plus, since BigDecimals are immutable, you don’t need to worry about creating new instances to do new calculations. As soon as you perform some operation on your totalCurrentSales instance, you’ll actually be creating a new BigDecimal and reassigning the totalCurrentSales reference to the new value.

From a instantiation perspective, they’re not necessarily exactly the same. In the OpenJDK 6b14 implementation, for example, BigDecimal.ZERO is created by invoking the private new BigDecimal(BigInteger, long, int) constructor with values BigInteger.ZERO, 0, and 0.

From a code quality perspective, using BigDecimal.ZERO is preferable to new BigDecimal(0) as you avoid the extra instantiation and having a literal in your code.

Leave a Comment