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);
}
How can I check if that value is null? I mean if it is not set I should print something else(say null)
I cannot check if its 0.0 because may be i can set the value to 0.0 also.
Thanks
Use Double instead of double, this will do exactly what you want
It sounds like you should be using
Double
(the class) rather thandouble
(the primitive). There's no such thing as anull
value of typedouble
:Note that you could make your setter take
double
instead ofDouble
if you wanted to prevent it from becomingnull
again after being set once.