检查double值是否为null(如果在Bean类中设置double值) [英] Check double value null or not(if double value set in Bean class)

查看:699
本文介绍了检查double值是否为null(如果在Bean类中设置double值)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个这样的Java Bean类

I have created a Java bean class like this

class BeanDemo
{
private double value;

//getter and setter
}

class myApp
{
BeanDemo beanDemo=new BeanDemo();

int val=7;
if(val<5)
{
   beanDemo.setValue(23.456);
}

double value=beanDemo.getValue(); // Always returns 0.0 if it is not set
System.out.println(value);
}

如何检查该值是否为空?我的意思是说如果没有设置我应该打印别的东西(比如说 null

How can I check if that value is null? I mean if it is not set I should print something else(say null)

我不能检查它的 0.0

I cannot check if its 0.0 because may be i can set the value to 0.0 also.

感谢

推荐答案

听起来你应该使用 Double (该类)而不是 double (原语)。没有这样的东西,例如 null 类型 double

It sounds like you should be using Double (the class) rather than double (the primitive). There's no such thing as a null value of type double:

class BeanDemo {
    private Double value;

    public void setValue(Double value) {
        this.value = value;
    }

    public Double getValue() {
        return value;
    }
}

class Test {
    public static void main(String[] args) {
        BeanDemo beanDemo = new BeanDemo();
        int val=7;
        if (val < 5) {
            beanDemo.setValue(23.456);
        }
        Double value = beanDemo.getValue(); // value will be null
        System.out.println(value);
    }
}

请注意,您可以让您的设置者 double 而不是 Double 如果你想防止它再次成为 null 被设置一次。

Note that you could make your setter take double instead of Double if you wanted to prevent it from becoming null again after being set once.

这篇关于检查double值是否为null(如果在Bean类中设置double值)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆