Does valueOf for BigInteger have any limitations ? I'm not sure but read somewhere, that given number can be of length = long only.
BigInteger
's valueOf()
method thakes a long
as its sole parameter. So the maximum number you can pass to it is the maximum a long
can represent (2^63-1 = 9223372036854775807
).
According to The Java API Specification for the BigInteger
class, the BigInteger.valueOf
method takes a long
as the argument, so the largest number that can be obtained through the BigInteger.valueOf
method is Long.MAX_VALUE
which is 2^63 - 1.
The BigInteger
class itself is used to represent immutable arbitrary-precision integers. Meaning it can represent integers of any size (limited of course by the memory on your computer).
However the valueOf
method returns a BigInteger
whose value is equal to that of the specified long. So a BigInteger
created in this way by definition can only be a large as Long.MAX_VALUE
BigInteger
objects created by the other methods and constructors of the BigInteger
class can of course be larger than Long.MAX_VALUE
.
Take for example the code snipped below:
BigInteger big1 = BigInteger.valueOf(Long.MAX_VALUE);
BigInteger big2 = BigInteger.valueOf(Long.MAX_VALUE);
BigInteger big3 = big1.add(big2);
The BigInteger
named big3
is larger than Long.MAX_VALUE
even though its constituent parts were created using the valueOf
method.
Consider using the BigInteger(String val, int radix)
constructor. That can create a BigInteger
of any size.