为什么 Java 的 BigInteger 有 TEN 和 ONE 作为常量?有什么实际用途吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17544151/
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
Why does Java's BigInteger have TEN and ONE as constants? Any Practical use?
提问by Harshal Patil
Why does BigInteger class has constant TEN and ONE? Any practical uses for having them as constants? I can understand some use cases for ZERO.
为什么 BigInteger 类具有常量 TEN 和 ONE?将它们作为常量有什么实际用途吗?我可以理解零的一些用例。
采纳答案by Mustafa Gen?
Let's say you have written a function that returns BigInteger
after some calculations and db operations. You often may need to return values like null, 0, 1. It's easy to return BigInteger.ZERO
. This values are public since they are needed commonly.
假设您编写了一个BigInteger
在一些计算和数据库操作后返回的函数。您可能经常需要返回 null、0、1 之类的值。很容易返回BigInteger.ZERO
。这个值是公共的,因为它们是普遍需要的。
public BigInteger findMyLovelyBigInteger(Integer secretNumber) {
if (secretNumber == null) {
return BigInteger.ZERO; // better than BigInteger.valueOf(0);
} else {
// some db operations
}
}
Also BigInteger.TEN
is commonly used for power/divide/mod operations.
也BigInteger.TEN
常用于功率/除法/调制操作。
Checkout BigInteger
public methods. You will easily see their common use cases.
结帐BigInteger
公共方法。您将很容易看到它们的常见用例。
回答by mikera
Oneis probably the most fundamental number in mathematics (alongside zero). Seems to deserve its own constant on that alone. Practically, it's a useful number to have as a constant in many algorithms.
一可能是数学中最基本的数字(与零一起)。仅凭这一点似乎就值得拥有自己的常数。实际上,在许多算法中将其作为常数是一个有用的数字。
Tenis important in the BigInteger context because it is the base of the decimal number system. This is important because several BigInteger functions need a radix base (e.g. BigInteger.toString(int radix)
. It's therefore a pretty useful constant to have.
十在 BigInteger 上下文中很重要,因为它是十进制数字系统的基础。这很重要,因为几个 BigInteger 函数需要一个基数(例如BigInteger.toString(int radix)
。因此它是一个非常有用的常量。
回答by PeterMmm
IMHO that was a design decision because 1 and 10 are used in other parts of the Java API code base. Creating Big* is an expensive operation and using constant avoid new object creation (Big* are immutable anyway) and the code is more readable.
恕我直言,这是一个设计决定,因为 1 和 10 用于 Java API 代码库的其他部分。创建 Big* 是一项昂贵的操作,使用常量避免创建新对象(Big* 无论如何都是不可变的)并且代码更具可读性。
Probably other constants weren't needed by the rest of the API.
API 的其余部分可能不需要其他常量。
Making them public was probably a fault. (The java API is full of "bad" design decisions)
将它们公开可能是一个错误。(Java API 充满了“糟糕”的设计决策)
回答by Puce
I don't know actually, but maybe:
我实际上不知道,但也许:
- zero: additive identity
- one: multiplicative identity
- ten: radix of the decimal system
Maybe they just used these constans a lot internally? (I haven't checked the source code, but it's Open Source).
也许他们只是在内部经常使用这些常量?(我没有检查源代码,但它是开源的)。
Edit:
编辑:
E.g.:
例如:
After looking at the code I think the reason is because they use ZERO and ONE a lot internally. Probably thought that others might profit from these constants as well given the reasons above. Or maybe they use these constants in some other classes as well.
查看代码后,我认为原因是因为他们在内部使用了很多零和一。鉴于上述原因,可能认为其他人也可能从这些常数中受益。或者他们也可能在其他一些类中使用这些常量。
TEN has been added in v1.5 It doesn't seem to be used internally, but maybe by another class?
TEN在v1.5中已经加入了,内部好像没有使用,但是可能是别的类使用的吧?
回答by user2563908
10, 0 and 1 can answer almost everything. for example if you want to arrive at 42, you can do a (10 + 10 + 10 + 10 + 1 + 1) :)
10、0 和 1 几乎可以回答所有问题。例如,如果你想达到 42,你可以做一个 (10 + 10 + 10 + 10 + 1 + 1) :)