如何在 Java 中保留 100 万位数字?

How can I keep 1 million digit in Java?

我的问题是,我想将 2 个有 100 万位的数字相乘。当我试图将 100 万个数字分配给 BigInteger 时,编译器给我错误。错误是:"constant string too long"。

BigInteger 确实是存储如此大的整数的方式,尽管数百或数千位数字是更典型的用例。但是,Java class 文件有一些限制,不允许硬编码那么大的文字数字。

相反,将数字存储在文件中并在运行时读取它。如果文件包含十进制、十六进制或其他一些基数的文本表示,您可以 read it into a String and pass it to the BigInteger constructor. If the file contains the raw bits, load it to a byte[] and use a different constructor.