Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making BigInteger equal another BigInteger variable in Java

Tags:

java

I'm wondering is there a way to change a BigInteger variable's value to another BigInteger's value? It seems like 'BigInteger.valueOf' doesn't accept other BigIntegers as its value and neither does 'new BigInteger'.

like image 841
Bun Avatar asked Dec 03 '25 09:12

Bun


1 Answers

I didn't read your question carefully enough.

I'm wondering is there a way to change a BigInteger variable's value to another BigInteger's value

No, since BigInteger is immutable.

However, you can create a new BigInteger instance that would have the same value as the original :

One way is to convert to String and construct the copy from that String :

BigInteger source = ...
BigInteger target = new BigInteger (source.toString());

Another way is to use the byte array :

BigInteger target = new BigInteger (source.toByteArray());
like image 59
Eran Avatar answered Dec 05 '25 21:12

Eran