Reputation: 44094
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
?
Upvotes: 35
Views: 50438
Reputation: 1154
If it's Java 7+ you can use
import java.util.Objects;
And
if (Objects.equals(someBool, true))
Upvotes: 0
Reputation: 8731
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>
Upvotes: 0
Reputation: 2586
It's old, but Boolean.valueOf(null)
is false
, just like Boolean.valueOf(false)
is false
.
Upvotes: 0
Reputation: 39397
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
Upvotes: 1
Reputation: 628
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.
Upvotes: 0
Reputation: 44094
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");
Upvotes: 3
Reputation: 89169
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.
Upvotes: 4
Reputation: 346300
If you want to handle Boolean
instances as well as primitives and be null-safe, you can use this:
if(Boolean.TRUE.equals(someBool))
Upvotes: 127
Reputation: 5887
As Boolean will give you an object, you must always check for NULL before working on the object
Upvotes: -1
Reputation: 597086
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)
Upvotes: 53