Tuesday, March 13, 2007

BigDecimal error:

As API indicates, it is "immutable".
If you do following, it is wrong:

BigDecimal a = new BigDecimal("0");
BigDecimal b = new BigDecimal("1");
a.add(b); // wrong

a will not be changed.

Notice that add() method return BigDecimal object, which value is "this+val",
then this should be:
a = a.add(b);

No comments:

Post a Comment