Java check if boolean is null
boolean
can only be true
or false
because it's a primitive datatype (+ a boolean
variables default value is false
). You can use the class Boolean
instead if you want to use null
values. Boolean is a reference type, that's the reason you can assign null
to a Boolean "variable". Example:
Boolean testvar = null;
if (testvar == null) { ...}
A boolean
cannot be null
in java.
A Boolean
, however, can be null
.
If a boolean
is not assigned a value (say a member of a class) then it will be false
by default.