98

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 : "";
Md. Sabbir Ahmed
  • 850
  • 8
  • 22
Delmon Young
  • 2,013
  • 5
  • 39
  • 51

8 Answers8

150

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) { ...}
Eich
  • 3,728
  • 1
  • 21
  • 34
  • So could I do something like: **Boolean hideInNav = parent.getProperties().get("hideInNav", false); if(hideInNav != null){ do something } ** – Delmon Young Apr 10 '13 at 22:47
  • 1
    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`. – Eich Apr 10 '13 at 22:50
  • 1
    BooleanUtils.isTrue(java.lang.Boolean bool) is what I use to check its status in the condition as Boolean can be null. https://commons.apache.org/proper/commons-lang/javadocs/api-2.4/org/apache/commons/lang/BooleanUtils.html – Youness Mar 08 '19 at 19:46
  • 1
    Without apache commons: ```Boolean.TRUE.equals(xxx)``` – Eich Mar 14 '19 at 12:00
82
  • 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.
informatik01
  • 16,038
  • 10
  • 74
  • 104
Trevor Freeman
  • 7,112
  • 2
  • 21
  • 40
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

Community
  • 1
  • 1
Joban
  • 1,318
  • 7
  • 19
  • 2
    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`. – Joshua Taylor Jun 24 '13 at 17:09
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.

Kenogu Labz
  • 1,094
  • 1
  • 9
  • 20
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.

Tom
  • 2,369
  • 13
  • 21
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.

christopher
  • 26,815
  • 5
  • 55
  • 89
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 ...}
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));
}
Ferdous Wahid
  • 3,227
  • 5
  • 27
  • 28