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

Jon Skeet
people
quotationmark

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.

people

See more on this question at Stackoverflow