Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android-java: check boolean value checking for null

Tags:

java

android

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.

Thanks

like image 336
Yogesh Avatar asked Oct 29 '11 11:10

Yogesh


1 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.

like image 192
Pablo Santa Cruz Avatar answered Nov 26 '22 05:11

Pablo Santa Cruz