Java check if boolean is null
Asked Answered
F

9

100

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 : "";
Fennec answered 10/4, 2013 at 22:35 Comment(0)
T
151

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) { ...}
Transferor answered 10/4, 2013 at 22:37 Comment(4)
So could I do something like: **Boolean hideInNav = parent.getProperties().get("hideInNav", false); if(hideInNav != null){ do something } **Fennec
Yes, because then hideInNav holds a reference to a Boolean object and can be null (no reference assigned). Therefor you can check if the reference is null.Transferor
BooleanUtils.isTrue(java.lang.Boolean bool) is what I use to check its status in the condition as Boolean can be null. commons.apache.org/proper/commons-lang/javadocs/api-2.4/org/…Stickybeak
Without apache commons: Boolean.TRUE.equals(xxx)Transferor
C
84
  • 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.
Crescentic answered 10/4, 2013 at 22:37 Comment(3)
Correct Explanation, Thanks.Drawback
for me this is not correct, boolean is giving nullDiseur
@صليعليمحمد-AtefFarouk, then you are either not working with Java or you mistook Boolean for boolean.Jenninejennings
J
12

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

Joanjoana answered 10/4, 2013 at 22:48 Comment(1)
The capitalization of Boolean in “A Boolean which…” is unfortunate. A variable of type boolean can only hold true and false, but a variable of type Boolean holds a Boolean object, or null.Blackamoor
A
8

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.

Anyaanyah answered 10/4, 2013 at 22:39 Comment(0)
L
8

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.

Laquanda answered 10/4, 2013 at 22:40 Comment(0)
B
4

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.

Burdensome answered 10/4, 2013 at 22:39 Comment(1)
Agreed. Not the best time for spelling mistakes! Swiftly editing :)Burdensome
M
0

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 ...}
Martinic answered 29/6, 2020 at 9:27 Comment(0)
Z
0

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));
}
Zack answered 21/6, 2023 at 11:8 Comment(0)
W
0

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.

Webworm answered 12/2 at 14:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.