布尔值 != false
在 Java 中,您通常会说
if(someBool != false)
相同
与if(someBool)
,但是如果 someBool
不是这样呢?类型为 boolean
但为 Boolean
,且其值为 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
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
由于 Boolean 会给你一个对象,因此在处理该对象之前你必须始终检查 NULL
As Boolean will give you an object, you must always check for NULL before working on the object
如果它为 null 那么你会得到一个 NullPointerException
If its null then you'll get a NullPointerException
实际上,布尔构造函数接受 null,返回 FALSE 并且不会抛出 NullPointerTantrum。
这样做的额外好处是还可以对字符串
"true"
给出真实的响应,而Boolean.TRUE.equals
则不然,但我们再次受到更多限制,只有字符串和布尔值的构造函数。您可以通过字符串连接来克服一些问题,这也是防空的。
确保 java 中可用的所有 TRUE 选项仍然保留。
Actually the Boolean constructor accepts null, returns FALSE and doesn't throw a NullPointerTantrum.
This has the added bonus of also giving a thruthy response to the string
"true"
which is not the case forBoolean.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.
Ensuring that all the TRUE options, available in java, still remains.
如果是 Java 7+ 你可以使用
And
If it's Java 7+ you can use
And
它很旧,但是
Boolean.valueOf(null)
是false
,就像Boolean.valueOf(false)
是false
一样>。It's old, but
Boolean.valueOf(null)
isfalse
, just likeBoolean.valueOf(false)
isfalse
.很好地说明了原始布尔值和布尔值之间的区别对象布尔。前者只能是真或假。后者可以是真、假或未知/未定义。 (即,空)。您使用哪个取决于您是要处理两个用例还是三个用例。
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.
但是,您可以将空布尔值与布尔实例进行比较。例如:
打印:
You can however compare a null Boolean with a Boolean instance. For example :
prints :
我做了一个小测试:
输出令人惊讶:
第一个 NPE 是可以预料的,因为 o 将被自动拆箱(并且失败,因为它为空)。第二次发生的原因相同,但感觉不自然。无论如何,解决方案是:
I did a little test:
The output is surprising:
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:
如果
someBool
为Boolean
由于 Boolean 可能为
null
,请确保通过检查是否为 null 来避免NullPointerException
。If
someBool
isBoolean
Since Boolean can be
null
make sure you avoidNullPointerException
by checking for not null.使用 ApacheCommons BooleanUtils.isTrue() 或 .isFalse()
Use ApacheCommons BooleanUtils.isTrue() or .isFalse()
它将抛出一个
NullPointerException
(null
的自动拆箱 会抛出 NPE)。但这仅意味着您不能允许
null
值。要么使用默认值,要么不使用自动拆箱并进行非空检查。因为使用布尔值的null
意味着您有 3 个值,而不是 2 个值。 (Michael 和 Tobiask 提出了更好的处理方法)It will throw a
NullPointerException
(autounboxing ofnull
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 anull
value of a boolean means you have 3, not 2 values. (Better ways of handling it were proposed by Michael and Tobiask)如果您想处理
Boolean
实例以及基元并且是 null 安全的,您可以使用:If you want to handle
Boolean
instances as well as primitives and be null-safe, you can use this: