Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BigInteger(long) has private access in BigInteger

I am trying to store a big calculation in a BigInteger instance. I tried this :

BigInteger answer=new BigInteger(2+3);

and got the following error :

temp.java:17: error: BigInteger(long) has private access in BigInteger
                        BigInteger answer=new BigInteger(2+3);
                                          ^
1 error

I know that instead of "2+3" there should be a string value. But i don't know how to satisfy that condition(assuming that i don't how much 2+3 is). Please tell me how to assign a calculated value to BigInteger object(assign 2+3 to BigInteger answer).

like image 536
Sunil Kumar Avatar asked Sep 29 '14 16:09

Sunil Kumar


People also ask

What is the difference between long and BigInteger?

BigInteger is capable of holding far bigger numbers than Long.

Is BigInt and long the same?

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.

Does BigInteger have a limit?

BigInteger has no cap on its max size (as large as the RAM on the computer can hold).

Is BigInt bigger than long?

3. BigInteger Larger Than Long. MAX_VALUE. As we already know, the long data type is a 64-bit two's complement integer.


1 Answers

If you want to perform the arithmetic using BigInteger, you should create a BigInteger for each value and then use BigInteger.add. However, you don't need to use strings to do that. You may want to if your input is already a string and it might be long, but if you've already got a long, you can use BigInteger.valueOf. For example:

BigInteger answer = BigInteger.valueOf(2).add(BigInteger.valueOf(3));

I certainly wouldn't convert a long into a String just to then pass it into the BigInteger constructor.

like image 170
Jon Skeet Avatar answered Oct 14 '22 19:10

Jon Skeet