java.math.MutableBigInteger
is only available from inside the package. It inherits from java.lang.Object
and there is only one subclass (Sign
The issue with BigInteger is that it's immutable: in other words, once you have a BigInteger object, you can't change the value of the object itself, you can only replace it with a new object.
Now this is normally a fine thing, since it prevents aliasing and so on (you don't want your "2 + 3" somewhere to suddenly turn into "2 + 5" because a user of that "3" somewhere else in your program changed it into a "5"). However, internally the BigInteger uses an array to hold the components of this value. For a large number, this array can be quite large; a BigInteger representing a bazillion might need an array of, oh, a thousand elements, say.
So what happens when I want to add one to this BigInteger? Well, we create a new BigInteger, which in turn will create a new array internal to it of a thousand elements, copy all of the elements of the old BigInteger's internal array to the new BigInteger's internal array, excepting the last one, and put in a new version of that last element incremented by one. (Or it might need to update the last two.) If you then don't need the old value, it frees up that old BigInteger, which frees up the array.
This is obviously pretty inefficient if you are just getting rid of the old values anyway. So if you have operations like this, you can instead use a MutableBigInteger, which can be incremented by simply changing the last element in the internal array of the existing MutableBigInteger. This is far faster! However, it does destroy the old value, which can be problematic, as I pointed out above. If someone gives you the int "3", you can expect that to stay the same. If someone gives you a MutableBigInteger, don't expect it to be the same number later on!