BigInteger +- 运算?
是否可以以某种方式进行这样的 +- 操作?
BigInteger a = new BigInteger("1");
BigInteger b = new BigInteger("2");
BigInteger result;
a+=b;
//result = a.add(b);
Is it possible to do +- operations like this somehow?
BigInteger a = new BigInteger("1");
BigInteger b = new BigInteger("2");
BigInteger result;
a+=b;
//result = a.add(b);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不幸的是没有。 Java 语言不支持运算符重载。该语法仅适用于通过自动装箱的其他数字基元包装器,这对于 BigInteger 没有意义,因为没有等效的基元。
Unfortunately not. Operator overloading is not supported in the Java language. The syntax only works for the other numeric primitive wrappers via auto-boxing, which wouldn't make sense for
BigInteger
as there's no equivalent primitive.一句话,不。 Java 中不存在运算符重载,并且
BigInteger
也不是编译器魔法支持+
和+=< 等运算符的特殊类型之一。 /代码>。
In a word, no. There is no operator overloading in Java, and
BigInteger
is not one of the special types for which there is compiler magic to support operators such as+
and+=
.不,不是在 Java 上。但是您可以看看其他 JVM 语言,例如 Groovy 或 Kotlin:两者都可以与 Java 的 BigIntegers 和 BigDecimal 类型互操作,并且都允许使用 +和
-
操作如您所愿。No, not on Java. But you could have a look at other JVM languages like Groovy or Kotlin: both are interoperable with Java's
BigIntegers
andBigDecimal
types and both allow using+
and-
operations as you desire.没有。
BigInteger
是不可变的,因此在创建之后就无法更改a
的值。通常的数学运算符也不适用于它们,因此您也不能执行a += b
。您需要执行您已注释掉的操作 -
result = a.add(b);
Nope.
BigInteger
s are immutable, so you can't change the value ofa
after creating it. And the usual mathematical operators don't work on them either, so you can't doa += b
either.You'd need to do what you have commented-out there --
result = a.add(b);