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

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

Default value of a BigInteger

javabiginteger

提问by Gaurav Varma

I have a code in which I am converting from Longto BigIntegerand vice-versa. While converting from Longto BigInteger, I want to make sure that there is no NullPointerException. So the code looks like this:

我有在我从转换的代码LongBigInteger,反之亦然。在从Longto转换时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 nullparameter:

参数有两个合理的返回值null

  • zero (BigIntegerprovides the constant BigInteger.ZEROyou could use for that)
  • 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 Longobject: you can use BigInteger.ZEROas default value:

假设myModel.getLongVariable()返回一个Long对象:您可以使用BigInteger.ZERO默认值:

myModel.getLongVariable()!=null)? BigInteger.valueOf(myModel.getLongVariable())
                                : BigInteger.ZERO;

Other constant fields of the BigIntegerclass are:

BigInteger该类的其他常量字段是:

  1. BigInteger.ONE: The BigInteger constant one
  2. BigInteger.TEN: The BigInteger constant ten.
  1. BigInteger.ONE: BigInteger 常量一
  2. 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() 这样的默认构造函数。

BigIntegeris 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"