<<< The BigDecimal arithmetic example | Index | Copying BigDecimals >>> |
BigDecimal objects are immutable -- if you create a BigDecimal with the value of 2.00, the object will remain 2.00 and can never be changed.
The methods such as .add(), .multiply(), and so on, return a new BigDecimal object containing the result.
Therefore, the following two lines demonstrate right and wrong ways to do the math with BigDecimal:
amountA = amountA.add( amountB ); // CORRECT -- new object is created amountA.add( amountB ); // INCORRECT -- attempting to update object "in place"
<<< The BigDecimal arithmetic example | Index | Copying BigDecimals >>> |