Java Long 与 BigInteger

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31748028/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 11:33:57  来源:igfitidea点击:

Long vs BigInteger

javalong-integerbiginteger

提问by Fritz Duchardt

I understand that both java.lang.Longand java.math.BigIntegercan hold very large natural numbers.

据我了解,双方java.lang.Longjava.math.BigInteger可以容纳非常大的自然数。

I also know Long's max value, but what is the max value for BigInteger?

我也知道 Long 的最大值,但 BigInteger 的最大值是多少?

And aside from capacity, would BigInteger ever perform better when working with generally large integers that still fall in Long's range?

除了容量之外,BigInteger 在处理仍在 Long 范围内的大整数时会表现得更好吗?

Question

Is the only consideration: is my value too large for Long?

唯一的考虑是:我的价值对Long来说太大了吗?

回答by AHungerArtist

BigInteger is capable of holding far bigger numbers than Long. BigInteger seems capable of holding (2 ^ 32) ^ Integer.MAX_VALUE, though that depends on the implementation (and, even if truly unbounded in the implementation, there will eventually be a physical resource limit) See explanation here.

BigInteger 能够容纳比 Long 大得多的数字。BigInteger 似乎能够保持 (2 ^ 32) ^ Integer.MAX_VALUE,尽管这取决于实现(并且,即使在实现中真正不受限制,最终也会有物理资源限制)请参阅此处的解释

The range of Longis [-9,223,372,036,854,775,808, +9,223,372,036,854,775,807].

的范围Long是 [-9,223,372,036,854,775,808, +9,223,372,036,854,775,807]。

Long will perform better than BigInteger so it really depends on what the size of your values will be. If they would all fall under Long's max value, it makes no sense not to use Long. If any would be bigger than the Long max value, you pretty much have to use BigInteger.

Long 会比 BigInteger 表现得更好,所以它真的取决于你的值的大小。如果它们都低于 Long 的最大值,那么不使用 Long 是没有意义的。如果任何大于 Long 最大值,您几乎必须使用 BigInteger。