Reputation: 86925
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);
Upvotes: 3
Views: 3118
Reputation: 2249
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.
Upvotes: 1
Reputation: 500913
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 +=
.
Upvotes: 4
Reputation: 3652
Nope. BigInteger
s 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);
Upvotes: 0
Reputation: 6728
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.
Upvotes: 5