BigInteger (long) has private access to BigInteger

I am trying to store a large calculation in a BigInteger instance. I've tried this:

BigInteger answer=new BigInteger(2+3);


and got the following error: error: BigInteger(long) has private access in BigInteger
                        BigInteger answer=new BigInteger(2+3);
1 error


I know there should be a string value instead of "2 + 3". But I don't know how to satisfy this condition (assuming I don't know how many 2 + 3 are). Tell me how to assign a computed value to a BigInteger object (assign the answer 2 + 3 BigInteger).


source to share

2 answers

If you want to do arithmetic with BigInteger

, you must create BigInteger

for each value and then use BigInteger.add

. However, you don't need to use strings for this. You might want if your input is already a string, and might be long, but if you already have one long

, you can use BigInteger.valueOf

. For example:

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


I certainly wouldn't convert to long

in String

order to pass it to the constructor BigInteger




You can just use the BigInteger method add(...)


BigInteger answer = new BigInteger("2").add(new BigInteger("3"));


Don't worry about overflow with this solution.



All Articles