Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BigInteger copying

Maybe I'm really confused because it seems a very simple question but Google and the official documentation were not enough.

I want to copy an BigInteger, and I can't find a clean way of doing it. BigInteger a = b when b is a BigInteger, as BigInteger is an object and there's no operator overloading here, a references to the same object as b

The cleanest way I've found so far is BigInteger a = b.add(BigInteger.ZERO), but still a dirty way for me.

like image 831
Carlos Ledesma Avatar asked Jan 08 '13 01:01

Carlos Ledesma


People also ask

What is BigInteger used for?

BigInteger provides analogues to all of Java's primitive integer operators, and all relevant methods from java. lang. Math. Additionally, BigInteger provides operations for modular arithmetic, GCD calculation, primality testing, prime generation, bit manipulation, and a few other miscellaneous operations.

Is BigInteger slow Java?

Add: BigInt is fastest due to its mutability. Apint is by far the slowest, since it uses a power of 10 base. Sub: The same goes here. Apint is slow when it comes to simple arithmetic operations.

Is BigInt same as long?

The equivalent of Java long in the context of MySQL variables is BigInt. In Java, the long datatype takes 8 bytes while BigInt also takes the same number of bytes.


2 Answers

BigInteger is immutable like other wrapper classes such as Integer and String. So no worries about possible alteration/tampering.

P.S: As a bonus, here's a link on when defensive copying is needed.

http://www.javapractices.com/topic/TopicAction.do?Id=15

like image 137
James P. Avatar answered Sep 19 '22 04:09

James P.


BigInteger is immutable -- there should never be any need to copy a BigInteger.

like image 32
Louis Wasserman Avatar answered Sep 20 '22 04:09

Louis Wasserman