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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 15:46:32  来源:igfitidea点击:

Boolean != false

javabooleanautoboxing

提问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 someBoolis not of type booleanbut Boolean, and its value is null?

但是如果someBool不是booleanbut 类型Boolean,而它的值为null呢?

采纳答案by Bozho

It will throw a NullPointerException(autounboxingof nullthrows NPE).

它会抛出一个NullPointerExceptionautounboxingnull抛出NPE)。

But that only means that you must not allow a nullvalue. Either use a default, or don't use autounboxing and make a non-null check. Because using a nullvalue 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 Booleaninstances as well as primitives and be null-safe, you can use this:

如果你想处理Boolean实例和基元并且是空安全的,你可以使用这个:

if(Boolean.TRUE.equals(someBool))

回答by Buhake Sindi

If someBoolis Boolean

如果someBoolBoolean

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

Since Boolean can be nullmake sure you avoid NullPointerExceptionby 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之间的区别。前者只能是。后者可以是truefalseunknown/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