Home » Android » android-java: check boolean value checking for null

android-java: check boolean value checking for null

Posted by: admin May 14, 2020 Leave a comment


I am trying for null check like below

if (isTrue == null)

compile error says : “The operator == is undefined for the argument type(s) boolean”

Please help, how to do null check.


How to&Answers:

You can’t do null check on primitive types. boolean is a primitive type.

If you absolutely need to represent a null value with a boolean variable, you need to use the wrapper class java.lang.Boolean.

So, your example would be:

Boolean isTrue;
isTrue = null; // valid
isTrue = true; // valid
isTrue = false; // valid
if (isTrue == null) {
    // valid!

Here’s the WIKIPEDIA entry for primitive wrapper classes.


The right way is

boolean isTrue;



You can not check if the boolean is null or not.boolean must be true or false.


A boolean is a primative type and cannot be null.


A boolean cannot be null in java.

A Boolean, however, can be null.