Java BigInteger 的默认值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19938673/
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
Default value of a BigInteger
提问by Gaurav Varma
I have a code in which I am converting from Long
to BigInteger
and vice-versa. While converting from Long
to BigInteger
, I want to make sure that there is no NullPointerException
. So the code looks like this:
我有在我从转换的代码Long
来BigInteger
,反之亦然。在从Long
to转换时BigInteger
,我想确保没有NullPointerException
. 所以代码看起来像这样:
(myModel.getLongVariable()!=null)?BigInteger.valueOf(myModel.getLongVariable()):**??**;
If you see the ??part, I am not sure what default value to put there. The BigInteger specificationsdon't provide a default constructor like new BigInteger()
.
如果你看到?? 部分,我不确定在那里放置什么默认值。该BigInteger的规格不提供一个默认的构造类似new BigInteger()
。
What everyone here suggests is the most obvious option i.e. using BigInteger.ZERO
. But zero is one of the values and would go accordingly in rest of the flow. I would basically write it in an xml and I am exploring if I can rather make it as blank instead of zero
这里每个人都建议的是最明显的选择,即使用BigInteger.ZERO
. 但是零是其中一个值,并且会在流程的其余部分中相应地进行。我基本上会把它写在一个 xml 中,我正在探索是否可以将它设为空白而不是零
Any further suggestion appreciated.
任何进一步的建议表示赞赏。
回答by MadConan
My suggestion: You give it a default value.
我的建议:你给它一个默认值。
BigInteger mydefault = BigInteger.ZERO;
回答by Bohemian
There are two sensible value to return for a null
parameter:
参数有两个合理的返回值null
:
- zero (
BigInteger
provides the constantBigInteger.ZERO
you could use for that) null
- 零(
BigInteger
提供BigInteger.ZERO
您可以使用的常量) null
A null is the closest thing to the "blank" you would like to return:
null 是最接近您想要返回的“空白”的东西:
return myModel.getLongVariable() != null
? BigInteger.valueOf(myModel.getLongVariable())
: null;
回答by Sage
assuming that myModel.getLongVariable()
is returning a Long
object: you can use BigInteger.ZERO
as default value:
假设myModel.getLongVariable()
返回一个Long
对象:您可以使用BigInteger.ZERO
默认值:
myModel.getLongVariable()!=null)? BigInteger.valueOf(myModel.getLongVariable())
: BigInteger.ZERO;
Other constant fields of the BigInteger
class are:
BigInteger
该类的其他常量字段是:
BigInteger.ONE
: The BigInteger constant oneBigInteger.TEN
: The BigInteger constant ten.
BigInteger.ONE
: BigInteger 常量一BigInteger.TEN
:BigInteger 常量十。
There is another constant, which is not mentioned inside the documentation for not letting anyone to know:
还有另一个常量,文档中没有提到它是为了不让任何人知道:
BigInteger.TWO
: has value of 2
, which is private
BigInteger.TWO
: 的值为2
,即private
Edit:
编辑:
The BigInteger specifications don't provide a default constructor like new BigInteger().
BigInteger 规范不提供像 new BigInteger() 这样的默认构造函数。
BigInteger
is a public class. Who is stopping you to extend and implement this constructor ?
BigInteger
是公开课。谁阻止你扩展和实现这个构造函数?
public class CBigInteger extends BigInteger {
public CBigInteger()
{
super("-12345678944546"); // testing with a default value
}
}
BigInteger cBig = new CBigInteger();
System.out.println(cBig.toString()); // prints "-12345678944546"