Search code examples
javabigdecimal

BigDecimal is not doing add operation


I have 2 BigDecimal numbers. I am trying to add them. My code is as follow:

BigDecimal bd1 = new BigDecimal(10);
BigDecimal bd2 = new BigDecimal(10);

bd1.add(bd2);

Here I am expecting the value of bd1 20 but again and again it's showing 10. It's not being added. Please help if I have done something wrong.


Solution

  • Reimeus is right. You need to assign the value to the result like this:

    bd1 = bd1.add(bd2);
    

    If you want to know details about immutable you can refer to following link:

    What is meant by immutable?