Luffy Singh Luffy Singh - 15 days ago 7
Java Question

The constructor BigInteger(long) is not visible

So I'm creating a factorial program using BigInteger class. But I keep getting the above error.

public static BigInteger fact(long n){
BigInteger result = BigInteger.ONE;
for(int i = 1; i <= n; ++i){
result = result.multiply(new BigInteger(i));
}
return result;
}


I already found the fix which is just add an empty string with result.

result = result.multiply(new BigInteger(i + ""))


My question is, why do we have to add that empty string ?

Answer

As per oracle docs, BigInteger does not have any constructor that takes int as an argument

Secondly you should use BigInteger.valueOf(i); instead of new BigInteger(i + "")