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

标签 java

java.math.MutableBigInteger 只能从包内部获得。它继承自 java.lang.Object,只有一个子类 (SignedMutableBigInteger),只能在包内使用。

最佳答案

/**
 * 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
 */

Source .

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

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

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

关于java - java.math.MutableBigInteger 的目的是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/890968/

相关文章:

java - CRudRepository native 查询无法返回结果集

java - Android, NDK, JNI, "cannot initialize a variable of type ' 长 *' with an rvalue of type ' jlong​​ *' "

java - TelnetClient 输出 - ANSI 禁用

java - 如何在 Spring 中连接相互依赖的 bean?

java - 如何在 Java 中使用 Apache Beam parDo 函数读取 JSON 文件

java - HashSet 包含重复条目

java - 当黑莓操作系统显示相机权限警报并且用户单击调用结束(红色)按钮时如何正确退出黑莓应用程序

Java流,创建自定义有状态操作

java - 正则表达式在 java 代码中不起作用,但在测试站点上不起作用

java - 在 JButton 中重写 PaintComponent() 会产生意外结果