java.math.MutableBigInteger的目的是什么?

10 java

java.math.MutableBigInteger只能从包装内部获得.它继承自java.lang.Object并且只有一个子类(SignedMutableBigInteger)只能从包内部获得.

Kev*_*ose 4

/**
 * A class used to represent multiprecision integers that makes efficient
 * use of allocated space by allowing a number to occupy only part of
 * an array so that the arrays do not have to be reallocated as often.
 * When performing an operation with many iterations the array used to
 * hold a number is only reallocated when necessary and does not have to
 * be the same size as the number it represents. A mutable number allows
 * calculations to occur on the same number without having to create
 * a new number for every step of the calculation as occurs with
 *  BigIntegers.
 *
 * @see BigInteger
 * @version 1.12, 12/19/03
 * @author Michael McCloskey
 * @since 1.3
 */
Run Code Online (Sandbox Code Playgroud)

来源

我猜想 MutableBigInteger 在内部用于 BigInteger 繁重的计算,这些计算因频繁的重新分配而减慢。我不确定为什么它不作为 java.math 的一部分导出。也许对可变值类有些厌恶?

为了澄清“可变”:
标准 BigInteger 在其整个生命周期内只有一个值,给定两个 BigInteger 引用“a”和“b”,“a+b”将始终产生一个具有相同值的新 BigInteger。假设该值为 4。

对于 MutableBigInteger,“a+b”最初可能会产生 4,但由于其他代码更改了“a”引用的对象的值(又名“变异”),因此在将来的某个时刻会产生 8、16、32 或任何其他数字。 ”和“b”。因此,Java 中的大多数(或许全部)值类型(Character、Short、Long、Integer、BigInteger、BigDecimal、Float、Double,甚至 String)都是不可变的。