Java check if boolean is null
JavaPerformanceBooleanNullableJava Problem Overview
How do you check if a boolean is null or not? So if I know "hideInNav" is null. How do I stop it from further executing? Something like the below doesn't seem to work but why?
boolean hideInNav = parent.getProperties().get("hideInNav", false);
String hideNavigation = hideInNav != null ? hideInNav : "";
Java Solutions
Solution 1 - Java
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) { ...}
Solution 2 - Java
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.
Solution 3 - Java
The only thing that can be a null
is a non-primivite.
A boolean
which can only hold TRUE
or FALSE
is a primitive. The TRUE
/FALSE
in memory are actually numbers (0
and 1
)
> 0 = FALSE > > 1 = TRUE
So when you instantiate an object it will be null
String str; // will equal null
On the other hand if you instaniate a primitive it will be assigned to 0 default.
boolean isTrue; // will be 0
int i; // will be 0
Solution 4 - Java
boolean
is a primitive type, and therefore can not be null.
Its boxed type, Boolean
, can be null.
The function is probably returning a Boolean
as opposed to a boolean
, so assigning the result to a Boolean
-type variable will allow you to test for nullity.
Solution 5 - Java
In Java, null
only applies to object references; since boolean
is a primitive type, it cannot be assigned null
.
It's hard to get context from your example, but I'm guessing that if hideInNav
is not in the object returned by getProperties()
, the (default value?) you've indicated will be false
. I suspect this is the bug that you're seeing, as false
is not equal to null
, so hideNavigation
is getting the empty string?
You might get some better answers with a bit more context to your code sample.
Solution 6 - Java
null
is a value assigned to a reference type. null
is a reserved value, indicating that a reference does not resemble an instance of an object.
A boolean
is not an instance of an Object. It is a primitive type, like int
and float
. In the same way that: int x
has a value of 0
, boolean x
has a value of false
.
Solution 7 - Java
boolean is a primitive data type in Java and primitive data types can not be null like other primitives int, float etc, they should be containing default values if not assigned.
In Java, only objects can assigned to null, it means the corresponding object has no reference and so does not contain any representation in memory.
Hence If you want to work with object as null , you should be using Boolean class which wraps a primitive boolean type value inside its object.
> These are called wrapper classes in Java
For Example:
Boolean bool = readValue(...); // Read Your Value
if (bool == null) { do This ...}