10

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

4 Answers 4

19

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.

Sign up to request clarification or add additional context in comments.

Comments

7

The right way is

boolean isTrue;
if(!isTrue)

or

if(isTrue)

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

Comments

4

A boolean is a primative type and cannot be null.

Comments

0

A boolean cannot be null in java.

A Boolean, however, can be null.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.