Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The constructor BigInteger(long) is not visible

Tags:

java

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 ?

like image 783
Aman Singh Avatar asked Dec 01 '22 16:12

Aman Singh


1 Answers

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 + "")

like image 167
SpringLearner Avatar answered Dec 05 '22 11:12

SpringLearner