membersound
membersound

Reputation: 86925

BigInteger +- operations?

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

Answers (4)

winne2
winne2

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

NPE
NPE

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

Jim Kiley
Jim Kiley

Reputation: 3652

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);

Upvotes: 0

SimonC
SimonC

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

Related Questions