了解 BigInteger 构造函数

Understanding BigInteger constructor

我的课本中有以下用于计算阶乘的代码:

 import java.math.*;
 public class LargeFactorial {
 public static void main(String[] args) {

  System.out.println("50! is \n" + factorial(50));
  }  public static BigInteger factorial(long n) {
      BigInteger result = BigInteger.ONE;
      for (int i = 1; i <= n; i++)
          result = result.multiply(new BigInteger(i +""));
      return result;
      }

不过,我真的不懂new BigInteger(i +"")。为什么他们把 +"" 放在构造函数中?我的意思是我们不是在乘以一个空字符串,它没有任何意义 either.Please 解释。

它只是调用 BigInteger(String) 构造函数,因为没有采用 int 的构造函数。使用字符串连接是一种将 int 转换为 String 的讨厌方法,但它会起作用。

IMO 更简洁的方法是使用 BigInteger.valueOf(long):

result = result.multiply(BigInteger.valueOf(i));

(考虑到这两个问题,我对你的教科书的质量有点担心...)