Categories
discuss

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

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 that’s a concern, but assuming I’m …

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 that’s a concern, but assuming I’m using 1.5 does it matter?

Thanks.

Answer

BigDecimal.ZERO is a predefined constant and therefore doesn’t have to be evaluated from a string at runtime as BigDecimal("0") would be. It will be faster and won’t require creation of a new object.

If your code needs to run on pre-1.5, then you can use the (much maligned) Singleton pattern to create an object equivalent to BigDecimal.ZERO. The first time it is used, it would call BigDecimal("0") to create a zero object, and return that object on subsequent calls. Otherwise, if your code is running on a 1.5 system, your singleton object can just return BigDecimal.ZERO with no runtime penalty.

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..