Java Boolean class variable used in comparison
I have
Boolean condition;
public Boolean isCondition() { return condition; }
Is the following use of this method used, as with primitive,
if (isCondition())
{
//...
}
I would use this for primitives, but not sure about the class. Do I need to check for NULL? Do I need getBooleanValue () first?
source to share
Yes, you can use any boxed primitive (in your case Boolean
) as the primitive itself; namely your example would work if you could guarantee that it condition
was set to a non-zero value.
Here caution is that if any of themselves boxed primitives null
, you'll getNullPointerException
when unpacking. If you are in a position to have this value null
, you must reevaluate why you are using the boxed primitive in the first place. It might be better to use the unboxed primitive (in your case Boolean
).
source to share