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
?
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
?
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)
If you want to handle Boolean
instances as well as primitives and be null-safe, you can use this:
if(Boolean.TRUE.equals(someBool))
null
as well, !Boolean.FALSE.equals(someBool)
–
Laxation Boolean.FALSE.equals(null)
will return false
- your statement does exactly the same as Michael's answer. –
Commentary !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 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)
Use ApacheCommons BooleanUtils.isTrue() or .isFalse()
Boolean.TRUE.equals(...)
, etc. Personally didn't think it was worth the 3rd party dep. –
Dextrose 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 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.
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");
Boolean.TRUE.equals(o)
–
Philosophism 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
assertThat(new Boolean(true) == Boolean.TRUE, is(true));
this test doesn't pass... –
Greenfield Boolean.TRUE.equals(myBool)
or Boolean.FALSE.equals(myBool)
instead. –
Lombroso 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.
It's old, but Boolean.valueOf(null)
is false
, just like Boolean.valueOf(false)
is false
.
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>
If it's Java 7+ you can use
import java.util.Objects;
And
if (Objects.equals(someBool, true))
As Boolean will give you an object, you must always check for NULL before working on the object
If its null then you'll get a NullPointerException
© 2022 - 2024 — McMap. All rights reserved.