what is the difference between below two lines of code?
BigDecimal one = new BigDecimal("1");
BigDecimal two = BigDecimal.ONE;
Are both the lines same?
Thanks!
valueOf() has the more intuitive behaviour, while new BigDecimal(d) has the more correct one. Try both and see the difference.
A BigDecimal consists of an arbitrary precision integer unscaled value and a 32-bit integer scale. If zero or positive, the scale is the number of digits to the right of the decimal point.
Field. static BigDecimal ONE − The value 1, with a scale of 0. static int ROUND_CEILING − Rounding mode to round towards positive infinity.
equals() method checks for equality of a BigDecimal value with the object passed. This method considers two BigDecimal objects equal if only if they are equal in value and scale.
No, they're not quite the same - new BigDecimal("1")
allocates a new object each time it's executed (and have to parse the value, too); BigDecimal.ONE
will use a reference to the same existing object each time.
As BigDecimal
is immutable, you can reuse an existing instance freely - so it makes sense to refer to a "pre-canned" object where you know what the value will be.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With