java 如何在java中检查双精度值的null

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

How to Check null for double type of value in java

java

提问by Sai Kishore Mani

I have A double type of variable. I want to check whether the variable is null or not. I read that double will never become null, They suggested me to change datatype double to Double. If I change double to Double I am Having performance issue.Here I need to check null pointer.

我有一个双重类型的变量。我想检查变量是否为空。我读到 double 永远不会变为 null,他们建议我将数据类型 double 更改为 Double。如果我将 double 更改为 Double 我遇到了性能问题。在这里我需要检查空指针。

double dbl=ParentBean.getChildBean().getDays();

How to check dblis null or not. Thanks in advance.

如何检查是否dbl为空。提前致谢。

回答by Ramkumar Pillai

** Default value for Primitive double is always 0.0d ref: http://www.c4learn.com/java/java-default-values/

** 原始双精度值的默认值始终为 0.0d 参考:http://www.c4learn.com/java/java-default-values/

so do not expect null in it

所以不要指望里面有 null

回答by Oghli

you can not check a primitive (double, float, long, int, char, short, byte, boolean) datatype for null.

您无法检查 的原始 ( double, float, long, int, char, short, byte, boolean) 数据类型null

primitives have a value always. you can only check references to Objects for null. there is a wrapper Object for each of the primitives (Double, Float, Long, Integer, ...) if you use these, then you can check for null.

原语总是有一个值。您只能检查对对象的引用是否为空。如果您使用这些原语 ( Double, Float, Long, Integer, ...),则每个基元都有一个包装对象,然后您可以检查是否为空。

Example:

例子:

double c = 2.16;
Double d = new Double(c);
if(d != null){
     System.out.println("not null double variable");
}

回答by Amit Bhati

You can check null for wrapper class of double.

您可以为 double 的包装类检查 null。

Change the double to it's wrapper class Doubleand check with == for null.

将 double 更改为它的包装类Double并检查 == 是否为空。

Double dbl=ParentBean.getChildBean().getDays();

Now you can do:-

现在你可以这样做:-

if(dbl==null)

or you can do something like below, if getDays() return type is Double:-

或者您可以执行以下操作,如果 getDays() 返回类型为 Double:-

double dbl=ParentBean.getChildBean().getDays()==null?0.00d:ParentBean.getChildBean().getDays();

Above statement I am checking for null, if true assigning sentinel value -1d to double dbl else ParentBean.getChildBean().getDays(). Assigning sentinel value is not right approach but.

上面的语句我正在检查 null,如果 true 将哨兵值 -1d 分配给 double dbl else ParentBean.getChildBean().getDays()。分配哨兵值不是正确的方法,而是。