不会溢出的变量

A Variable That Doesn't Overflow

我最近一直在处理一些非常大的数字,它已经到了连我的多头都溢出的程度。有没有永远不会溢出的变量?

也许你应该使用 BigInteger,但它是 class 类型,而不是变量。

BigInteger bi = BigInteger.valueOf(num);

BigInteger解决了这里的问题。

Immutable arbitrary-precision integers. All operations behave as if BigIntegers were represented in two's-complement notation (like Java's primitive integer types). BigInteger provides analogues to all of Java's primitive integer operators, and all relevant methods from java.lang.Math. Additionally, BigInteger provides operations for modular arithmetic, GCD calculation, primality testing, prime generation, bit manipulation, and a few other miscellaneous operations. (Source)

您可以像这样创建 BigInteger

BigInteger int1 = new BigInteger("2");
BigInteger int2 = BigInteger.valueOf(4);

// However, because Java has no operator overloading, you have to do this.
// int3 would be 6.
BigInteger int3 = int1.add(int2);