java BigInteger.valueOf() 限制
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2101270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
BigInteger.valueOf() limits
提问by jarekg
Does valueOf for BigInteger have any limitations ? I'm not sure but read somewhere, that given number can be of length = long only.
BigInteger 的 valueOf 有任何限制吗?我不确定但在某处阅读,给定的数字只能是 length = long。
回答by Tendayi Mawushe
The BigIntegerclass 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).
在BigInteger类本身是用来表示不可变的任意精度的整数。这意味着它可以表示任何大小的整数(当然受计算机内存的限制)。
However the valueOfmethod returns a BigIntegerwhose value is equal to that of the specified long. So a BigIntegercreated in this way by definition can only be a large as Long.MAX_VALUE
但是,该valueOf方法返回BigInteger值等于指定 long 的值。因此BigInteger,根据定义以这种方式创建的只能是大为Long.MAX_VALUE
BigIntegerobjects created by the other methods and constructorsof the BigIntegerclass can of course be larger than Long.MAX_VALUE.
BigInteger对象由另一产生方法和构造所述的BigInteger类当然可以是大于Long.MAX_VALUE。
Take for example the code snipped below:
以下面截取的代码为例:
BigInteger big1 = BigInteger.valueOf(Long.MAX_VALUE);
BigInteger big2 = BigInteger.valueOf(Long.MAX_VALUE);
BigInteger big3 = big1.add(big2);
The BigIntegernamed big3is larger than Long.MAX_VALUEeven though its constituent parts were created using the valueOfmethod.
的BigInteger命名big3是大于Long.MAX_VALUE即使其组成份使用所创建的valueOf方法。
回答by Johannes Weiss
BigInteger's valueOf()method thakes a longas its sole parameter. So the maximum number you can pass to it is the maximum a longcan represent (2^63-1 = 9223372036854775807).
BigInteger的valueOf()方法将 along作为其唯一参数。因此,您可以传递给它的最大数字是 along可以表示的最大值( 2^63-1 = 9223372036854775807)。
回答by coobird
According to The Java API Specificationfor the BigIntegerclass, the BigInteger.valueOfmethod takes a longas the argument, so the largest number that can be obtained through the BigInteger.valueOfmethod is Long.MAX_VALUEwhich is 2^63 - 1.
据的的Java API规范的BigInteger类,该BigInteger.valueOf方法以一个long作为参数,以便能够通过获得最大数量BigInteger.valueOf的方法是Long.MAX_VALUE为2 ^ 63 - 1。
回答by finnw
Consider using the BigInteger(String val, int radix)constructor. That can create a BigIntegerof any size.
考虑使用BigInteger(String val, int radix)构造函数。这可以创建BigInteger任何大小的。

