BigInteger.valueOf() 限制
BigInteger 的 valueOf 有任何限制吗?我不确定,但在某处读过,给定的数字只能是 length = long 。
Does valueOf for BigInteger have any limitations ? I'm not sure but read somewhere, that given number can be of length = long only.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
BigInteger 类本身用于表示不可变的任意精度整数。这意味着它可以表示任何大小的整数(当然受计算机内存的限制)。
然而,
valueOf
方法返回一个BigInteger
,其值等于指定的 long 值。因此,根据定义以这种方式创建的BigInteger
只能与其他 BigInteger 的方法和构造函数 > class 当然可以大于
Long.MAX_VALUE
。以下面截取的代码为例:
名为
big3
的BigInteger
大于Long.MAX_VALUE
,即使其组成部分是使用创建的>valueOf
方法。The
BigInteger
class itself is used to represent immutable arbitrary-precision integers. Meaning it can represent integers of any size (limited of course by the memory on your computer).However the
valueOf
method returns aBigInteger
whose value is equal to that of the specified long. So aBigInteger
created in this way by definition can only be a large asLong.MAX_VALUE
BigInteger
objects created by the other methods and constructors of theBigInteger
class can of course be larger thanLong.MAX_VALUE
.Take for example the code snipped below:
The
BigInteger
namedbig3
is larger thanLong.MAX_VALUE
even though its constituent parts were created using thevalueOf
method.BigInteger
的valueOf()
方法将long
作为其唯一参数。因此,您可以传递给它的最大数字是long
可以表示的最大值 (2^63-1 = 9223372036854775807
)。BigInteger
'svalueOf()
method thakes along
as its sole parameter. So the maximum number you can pass to it is the maximum along
can represent (2^63-1 = 9223372036854775807
).根据 Java API 规范 的
BigInteger
类,BigInteger.valueOf
< /a> 方法以long
作为参数,因此通过BigInteger.valueOf
方法可以获得的最大数字为Long.MAX_VALUE
为 2^63 - 1.According to The Java API Specification for the
BigInteger
class, theBigInteger.valueOf
method takes along
as the argument, so the largest number that can be obtained through theBigInteger.valueOf
method isLong.MAX_VALUE
which is 2^63 - 1.考虑使用 BigInteger(String val, int radix) 构造函数。这可以创建任意大小的
BigInteger
。Consider using the
BigInteger(String val, int radix)
constructor. That can create aBigInteger
of any size.