Boolean != false
Asked Answered
L

12

35

In Java, you would usually say that

if(someBool != false)

is the same as

if(someBool)

But what if someBool is not of type boolean but Boolean, and its value is null?

Laxation answered 30/11, 2010 at 12:4 Comment(2)
Why don't you try it out yourself?Commentary
@Jesper: I did, see my own answer belowLaxation
P
53

It will throw a NullPointerException (autounboxing of null throws NPE).

But that only means that you must not allow a null value. Either use a default, or don't use autounboxing and make a non-null check. Because using a null value of a boolean means you have 3, not 2 values. (Better ways of handling it were proposed by Michael and Tobiask)

Phyte answered 30/11, 2010 at 12:8 Comment(0)
R
127

If you want to handle Boolean instances as well as primitives and be null-safe, you can use this:

if(Boolean.TRUE.equals(someBool))
Resolve answered 30/11, 2010 at 12:8 Comment(5)
Or if you want to do something when someBool is null as well, !Boolean.FALSE.equals(someBool)Laxation
@Bart Boolean.FALSE.equals(null) will return false - your statement does exactly the same as Michael's answer.Commentary
@Jesper: Nope. !Boolean.FALSE.equals(null) returns true while Boolean.TRUE.equals(null) returns false. However, this does demonstrate nicely that double negations are hard to parse mentally and therefore are better avoided.Resolve
In my case I have a Boolean doFoo. An error is thrown if doFoo is true and foo is not done, or if doFoo is false and foo is done. If doFoo is null no errors are thrown regardlessLaxation
@Michael, @Bart Argh, you're right, I even tried it out but made a mistake while doing so... sorry.Commentary
P
53

It will throw a NullPointerException (autounboxing of null throws NPE).

But that only means that you must not allow a null value. Either use a default, or don't use autounboxing and make a non-null check. Because using a null value of a boolean means you have 3, not 2 values. (Better ways of handling it were proposed by Michael and Tobiask)

Phyte answered 30/11, 2010 at 12:8 Comment(0)
C
20

Use ApacheCommons BooleanUtils.isTrue() or .isFalse()

Circosta answered 30/11, 2010 at 12:6 Comment(3)
This is a great way to go. The ApacheCommons libraries make Java programming a joy.Lathy
glancing at the code in lang3, this does Boolean.TRUE.equals(...), etc. Personally didn't think it was worth the 3rd party dep.Dextrose
@Dextrose not if you just want to avoid Boolean.TRUE.equals(myValue) but if you're already using the Apache Commons library for other stuff, then why not take advantage of it even more?Deadman
I
4

If someBool is Boolean

if (someBull != null && someBull) {
  //Yeah, true.
}

Since Boolean can be null make sure you avoid NullPointerException by checking for not null.

Isochromatic answered 30/11, 2010 at 12:10 Comment(0)
L
3

I did a little test:

    Boolean o = null;
    try {
        System.out.println(o ? "yes" : "no");
    } catch (Exception e) {
        e.printStackTrace();
    }
    try {
        System.out.println((o != false) ? "yes" : "no");
    } catch (Exception e) {
        e.printStackTrace();
    }

The output is surprising:

java.lang.NullPointerException
    at btest.main(btest.java:10)
java.lang.NullPointerException
    at btest.main(btest.java:15)

The first NPE is to be expected, because o will be autounboxed (and that fails because it's null). The second happens for the same reason, but it doesn't feel natural. Anyway, the solution is to do:

System.out.println(!Boolean.FALSE.equals(o) ? "yes" : "no");
Laxation answered 30/11, 2010 at 12:10 Comment(2)
You can avoid the double negation by saying Boolean.TRUE.equals(o)Philosophism
@ChrisParton no, you can not actualy. See the comments on Michael Borgwardt answer for difference in null caseStockade
B
1

You can however compare a null Boolean with a Boolean instance. For example :

Boolean myBool = null;
System.out.println(myBool == Boolean.FALSE);
System.out.println(myBool == Boolean.TRUE);

prints :

false
false
Bravery answered 27/5, 2013 at 8:51 Comment(3)
This won't work if I create a Boolean with new, it will work only when valueOf and possibly autoboxing is used assertThat(new Boolean(true) == Boolean.TRUE, is(true)); this test doesn't pass...Greenfield
@stivlo: I would have though Booleans (and all primitive-representing objects) would have been in some form of pool and considered equals. Thank you for the comment, I learnt something today.Bravery
use Boolean.TRUE.equals(myBool) or Boolean.FALSE.equals(myBool) instead.Lombroso
S
0

Good illustrations of the difference between the primitive boolean & the object Boolean. The former can be only true or false. The latter can be true, false, or unknown/undefined. (i.e., null). Which you use depends on whether you want to deal with two use cases or three.

Stench answered 1/12, 2010 at 16:54 Comment(1)
Unfortunately there are other reasons one would want to use Boolean, for instance storing the values a collection of Objects. In that case you have to deal with the three use cases whether you want them or not.Piercing
A
0

It's old, but Boolean.valueOf(null) is false, just like Boolean.valueOf(false) is false.

Alviani answered 8/8, 2017 at 18:13 Comment(0)
P
0

Actually the Boolean constructor accepts null, returns FALSE and doesn't throw a NullPointerTantrum.

 new Boolean(null);
 <false>

This has the added bonus of also giving a thruthy response to the string "true" which is not the case for Boolean.TRUE.equals but we are more restricted again having only constructors for Strings and Booleans.

Something you can overcome with string concatenation, which is also null-proof.

 new Boolean(""+null);
 <false>

 new Boolean(""+false);
 <false>

 new Boolean(""+new Object());
 <false>

 new Boolean(""+6);
 <false>

 new Boolean(""+new Integer(9));
 <false>

Ensuring that all the TRUE options, available in java, still remains.

 new Boolean(""+true);
 <true>

 new Boolean(""+"true");
 <true>
Plosion answered 2/9, 2017 at 0:39 Comment(0)
T
0

If it's Java 7+ you can use

import java.util.Objects;

And

if (Objects.equals(someBool, true))
Tritheism answered 27/4, 2018 at 21:58 Comment(0)
S
-1

As Boolean will give you an object, you must always check for NULL before working on the object

Synapse answered 30/11, 2010 at 12:8 Comment(0)
P
-2

If its null then you'll get a NullPointerException

Postconsonantal answered 30/11, 2010 at 12:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.