Java 布尔值 != 假
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4313513/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Boolean != false
提问by Bart van Heukelom
In Java, you would usually say that
在 Java 中,你通常会说
if(someBool != false)
if(someBool != false)
is the same as
是相同的
if(someBool)
if(someBool)
But what if someBool
is not of type boolean
but Boolean
, and its value is null
?
但是如果someBool
不是boolean
but 类型Boolean
,而它的值为null
呢?
采纳答案by Bozho
It will throw a NullPointerException
(autounboxingof null
throws NPE).
它会抛出一个NullPointerException
(autounboxing的null
抛出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)
但这仅意味着您不能允许null
值。要么使用默认值,要么不使用自动拆箱并进行非空检查。因为使用null
布尔值意味着您有 3 个值,而不是 2 个值。(更好的处理方法是由 Michael 和 Tobiask 提出的)
回答by Tobias
Use ApacheCommons BooleanUtils.isTrue() or .isFalse()
使用 ApacheCommons BooleanUtils.isTrue() 或 .isFalse()
回答by Gopi
As Boolean will give you an object, you must always check for NULL before working on the object
由于 Boolean 会为您提供一个对象,因此您必须在处理该对象之前始终检查 NULL
回答by Michael Borgwardt
If you want to handle Boolean
instances as well as primitives and be null-safe, you can use this:
如果你想处理Boolean
实例和基元并且是空安全的,你可以使用这个:
if(Boolean.TRUE.equals(someBool))
回答by Buhake Sindi
If someBool
is Boolean
如果someBool
是Boolean
if (someBull != null && someBull) {
//Yeah, true.
}
Since Boolean can be null
make sure you avoid NullPointerException
by checking for not null.
由于布尔值可以通过检查非空来null
确保您避免NullPointerException
使用。
回答by PaulJWilliams
If its null then you'll get a NullPointerException
如果它为 null 那么你会得到一个 NullPointerException
回答by Bart van Heukelom
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:
第一个 NPE 是可以预料的,因为 o 将被自动拆箱(并且因为它为空而失败)。第二个发生的原因相同,但感觉不自然。无论如何,解决方案是这样做:
System.out.println(!Boolean.FALSE.equals(o) ? "yes" : "no");
回答by aksarben
Good illustrations of the difference between the primitive boolean& the object Boolean. The former can be only trueor 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.
很好地说明了原始boolean和对象Boolean之间的区别。前者只能是真或假。后者可以是true、false或unknown/undefined。(即,空)。您使用哪种取决于您是要处理两个用例还是三个用例。
回答by njzk2
You can however compare a null Boolean with a Boolean instance. For example :
但是,您可以将 null Boolean 与 Boolean 实例进行比较。例如 :
Boolean myBool = null;
System.out.println(myBool == Boolean.FALSE);
System.out.println(myBool == Boolean.TRUE);
prints :
印刷 :
false
false
回答by Dave
It's old, but Boolean.valueOf(null)
is false
, just like Boolean.valueOf(false)
is false
.
这是老了,但Boolean.valueOf(null)
就是false
,就像Boolean.valueOf(false)
IS false
。