Reputation: 2053
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 : "";
Upvotes: 102
Views: 351349
Reputation: 1
You can use
Boolean.TRUE.equals(Boolean field)
it should be safety for null also.
Upvotes: 0
Reputation: 19
In my case, I have one or several boolean params in a Maven plugin, which need to be assigned to related class fields. For other @Param fileds I've used !=null condition and tried to found smth for boolean. So, I created an initializeParams() method to assign @Param values to private class fields. Solved by checking if at least one of non-boolean @Params !=null. Needed all this to write some unit tests for Java implementation of goals of Maven plugins.
Upvotes: 0
Reputation: 3367
You can use Objects.equals()
from java.util.Objects
if(Objects.equals(value1, value1){
//your code
}
In documentation it said, this will check null and value as well.
Implementation from Java doc:
Returns true if the arguments are equal to each other and false otherwise.
Consequently, if both arguments are null, true is returned and if exactly one
argument is null, false is returned. Otherwise, equality is determined by using
the equals method of the first argument.
Params: a – an object
b – an object to be compared with a for equality
Returns: true if the arguments are equal to each other and false otherwise
See Also: Object.equals(Object)
public static boolean equals(Object a, Object b) {
return (a == b) || (a != null && a.equals(b));
}
Upvotes: 0
Reputation: 7232
boolean
cannot be null
in Java.Boolean
, however, can be null
.boolean
is not assigned a value (say a member of a class)
then it will be false
by default.Upvotes: 87
Reputation: 91
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 ...}
Upvotes: 0
Reputation: 3788
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) { ...}
Upvotes: 154
Reputation: 1337
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
Upvotes: 12
Reputation: 27346
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
.
Upvotes: 4
Reputation: 2389
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.
Upvotes: 8
Reputation: 1134
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.
Upvotes: 9