BigInteger +- 运算?

发布于 2025-01-08 15:26:04 字数 172 浏览 2 评论 0原文

是否可以以某种方式进行这样的 +- 操作?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

寂寞清仓 2025-01-15 15:26:04

不幸的是没有。 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.

风流物 2025-01-15 15:26:04

一句话,不。 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 +=.

这个俗人 2025-01-15 15:26:04

不,不是在 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 and BigDecimal types and both allow using + and - operations as you desire.

嘿哥们儿 2025-01-15 15:26:04

没有。 BigInteger 是不可变的,因此在创建之后就无法更改 a 的值。通常的数学运算符也不适用于它们,因此您也不能执行 a += b

您需要执行您已注释掉的操作 - result = a.add(b);

Nope. BigIntegers are immutable, so you can't change the value of a after creating it. And the usual mathematical operators don't work on them either, so you can't do a += b either.

You'd need to do what you have commented-out there -- result = a.add(b);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文