In java , BigInteger is immutable but I want to understand why because a lot of times it is used to do a lot of calculations which can produce a lot of objects. So , it feels kind of intuitive to not make it immutable. The situation which comes to my mind is something like of string operations and then the option of StringBuilder. Should there be non-immutable counterpart of BigInteger ? I think it might be beneficial in a lot of situations.
Edit: I know the benefits of immutability and how it's beneficial in many cases. I just wanted to understand the benefits w.r.t BigInteger. I have used BigInteger to calculate factorial of large numbers. So , I would have preferred a mutable BigInteger. Similarly , BigInteger will be used for calculations where the result is much larger than int. For other cases there is BigDecimal.
Effective Java by Josh Bloch explains the benefits of immutable classes, in "Item 15: Minimize Mutability":
An immutable object is simple, because the object can only exist in one state -- the state it was created in. Simple code tends to have fewer bugs. Because the object cannot be modified, it is also thread-safe without external synchronization.
Bloch addresses the performance problem with immutable classes, using
BigInteger
as an example:So internally,
BigInteger
already does some optimization for you. If you really want a mutableBigInteger
, you can use aBitSet
, but note that this probably makes your code more complex -- and more error-prone. You should use the simplest possible solution (BigInteger
) unless you are confident that using a mutable version will give you a noticeable performance gain (see also "Item 55: Optimize judiciously" from the same book).