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).
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.
You could just use BigInteger's add(...)
method:
BigInteger answer = new BigInteger("2").add(new BigInteger("3"));
No worries about overflow with this solution.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.