java - 不会溢出的变量

标签 java overflow long-integer

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

最佳答案

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);

关于java - 不会溢出的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39862894/

相关文章:

css - WebKit 和溢出 : hidden affecting width

html - CSS/HTML - 浏览器调整大小时单列布局溢出

c - 存储(和打印)整数的最大数据类型是什么?

Java FlowLayout - 特定元素的边距/填充?

java - 电话资费计算器

java - 如何保存java程序的状态并在以后调用它?

c - 长长C型分割线

java - Mojo 添加 eclipse 源文件夹

html - flexbox 中的 Overflow:auto 对于 display:block 和 display:flex 的 child 表现不同

java - 转换为 char 数组后 Long 变量出现意外值