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).
BigInteger is capable of holding far bigger numbers than 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.
BigInteger has no cap on its max size (as large as the RAM on the computer can hold).
3. BigInteger Larger Than Long. MAX_VALUE. As we already know, the long data type is a 64-bit two's complement integer.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With