布尔值 != false

发布于 2024-10-04 18:26:10 字数 225 浏览 1 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

若水微香 2024-10-11 18:27:06

由于 Boolean 会给你一个对象,因此在处理该对象之前你必须始终检查 NULL

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

感情旳空白 2024-10-11 18:27:06

如果它为 null 那么你会得到一个 NullPointerException

If its null then you'll get a NullPointerException

雨夜星沙 2024-10-11 18:27:05

实际上,布尔构造函数接受 null,返回 FALSE 并且不会抛出 NullPointerTantrum。

 new Boolean(null);
 <false>

这样做的额外好处是还可以对字符串 "true" 给出真实的响应,而 Boolean.TRUE.equals 则不然,但我们再次受到更多限制,只有字符串和布尔值的构造函数。

您可以通过字符串连接来克服一些问题,这也是防空的。

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

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

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

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

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

确保 java 中可用的所有 TRUE 选项仍然保留。

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

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

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>
生生漫 2024-10-11 18:27:05

如果是 Java 7+ 你可以使用

import java.util.Objects;

And

if (Objects.equals(someBool, true))

If it's Java 7+ you can use

import java.util.Objects;

And

if (Objects.equals(someBool, true))
久夏青 2024-10-11 18:27:04

它很旧,但是 Boolean.valueOf(null)false,就像 Boolean.valueOf(false)false 一样>。

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

郁金香雨 2024-10-11 18:27:03

很好地说明了原始布尔值和布尔值之间的区别对象布尔。前者只能是。后者可以是未知/未定义。 (即,空)。您使用哪个取决于您是要处理两个用例还是三个用例。

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.

风吹过旳痕迹 2024-10-11 18:26:56

但是,您可以将空布尔值与布尔实例进行比较。例如:

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

打印:

false
false

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
雨轻弹 2024-10-11 18:26:40

我做了一个小测试:

    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();
    }

输出令人惊讶:

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

第一个 NPE 是可以预料的,因为 o 将被自动拆箱(并且失败,因为它为空)。第二次发生的原因相同,但感觉不自然。无论如何,解决方案是:

System.out.println(!Boolean.FALSE.equals(o) ? "yes" : "no");

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");
入画浅相思 2024-10-11 18:26:27

如果 someBoolBoolean

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

由于 Boolean 可能为 null,请确保通过检查是否为 null 来避免 NullPointerException

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.

姐不稀罕 2024-10-11 18:26:21

使用 ApacheCommons BooleanUtils.isTrue() 或 .isFalse()

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

抹茶夏天i‖ 2024-10-11 18:26:18

它将抛出一个 NullPointerException ( null 的自动拆箱 会抛出 NPE)。

但这仅意味着您不能允许 null 值。要么使用默认值,要么不使用自动拆箱并进行非空检查。因为使用布尔值的 null 意味着您有 3 个值,而不是 2 个值。 (Michael 和 Tobiask 提出了更好的处理方法)

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)

痕至 2024-10-11 18:26:15

如果您想处理 Boolean 实例以及基元并且是 null 安全的,您可以使用:

if(Boolean.TRUE.equals(someBool))

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

if(Boolean.TRUE.equals(someBool))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文