java Java中的“Boolean.TRUE.equals(x)”有什么原因吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12775082/
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-10-31 10:09:36  来源:igfitidea点击:

Is there any reason for "Boolean.TRUE.equals(x)" in Java?

javaboolean-expression

提问by user1508893

I've come across this code in one of the projects I'm working on

我在我正在从事的项目之一中遇到了这段代码

(This is in Java)

(这是在Java中)

if (Boolean.TRUE.equals(foo.isBar()))

Foo#isBar()is defined as boolean isBar(), so it can't return null

Foo#isBar()被定义为boolean isBar(),所以它不能返回null

Is there really any reason why it should be written that way? I myself would just write

真的有什么理由让它这样写吗?我自己只会写

if (foo.isBar())

, but perhaps I'm missing something subtle.

,但也许我错过了一些微妙的东西。

Thanks

谢谢

采纳答案by Kevin

Since isBarreturns a primitive boolean, there is no semantic difference. Additionally, the second way is more concise, more clear, and more efficient, since the result won't have to be autboxed for the call and then have the original boolean extracted again. Given all that, there is no reason to use the first method, and several to use the second, so use the second. I give a great deal of leeway to fellow coders, but I would sit down and have a chat with anyone who added something like that to professional code.

由于isBar返回一个原语boolean,没有语义差异。此外,第二种方式更简洁、更清晰、更高效,因为不必为调用自动装箱结果,然后再次提取原始布尔值。鉴于所有这些,没有理由使用第一种方法,有几个方法可以使用第二种方法,所以使用第二种方法。我给其他编码人员很大的回旋余地,但我会坐下来与任何在专业代码中添加类似内容的人聊天。

回答by Yogendra Singh

I hope foo.isBar()returns a boolean. In that case you can always write if (foo.isBar()). If you foo.isBar()returns Booleanthen it can be either Boolean.TRUE, Boolean.FALSEor NULL. In that case if (Boolean.TRUE.equals(foo.isBar()))makes sure the if block is executed in one scenario(TRUE) and omitted in remaining 2.

我希望foo.isBar()返回一个布尔值。在这种情况下,您始终可以编写if (foo.isBar()). 如果您foo.isBar()返回,Boolean则它可以是Boolean.TRUE,Boolean.FALSENULL。在这种情况下if (Boolean.TRUE.equals(foo.isBar())),请确保 if 块在一个场景(TRUE)中执行,并在剩余的 2 个场景中省略。

Over and aboveif (foo.isBar())will fail, whenfoo.isBar()returns Boolean NULL.

以上if (foo.isBar())将失败,当foo.isBar()返回布尔值 NULL 时。

回答by Kevin

I would suspect "old legacy code with no good reason" - and in fact, I would contend it is worse. (I wonder how ints are compared ..)

我会怀疑“没有充分理由的旧遗留代码” - 事实上,我认为它更糟。(我想知道如何int比较 ..)

The code that uses TRUE.equalsrequires a boxing conversion, an additional method call (and everything inside) and, in the end, it just looks sloppy.

使用的代码TRUE.equals需要一个装箱转换,一个额外的方法调用(以及里面的所有东西),最后,它看起来很草率



The only reason I am aware of is if foo.isBarwastyped as returning Boolean(not boolean) and where it may return null:

我知道的唯一原因是 iffoo.isBar输入为返回Boolean(not boolean) 以及它可能返回的位置null

Boolean b = null;

// throws an exception when it tries to unbox b because it is null
boolean isTrue1 = (boolean)b;

// evaluates to false
boolean isTrue2 = Boolean.TRUE.equals(b);

// evaluates to false as well
boolean isTrue3 = b != null ? (boolean)b : false;

回答by lockstock

Some people believe (myself not being one of them) that being overly explicit makes boolean conditions more readable. For example using

有些人认为(我自己不是其中之一)过于明确会使布尔条件更具可读性。例如使用

if(foo == true)instead of if(foo)

if(foo == true)代替 if(foo)

perhaps this is a similar case?

也许这是一个类似的案例?

回答by rbhawsar

in the first condition you are checking for the equality of Boolean object corresponding to true. and you are using the first condition in your code because your java version doesn't support autounboxing hence you need to use the boolean object.

在第一个条件中,您正在检查与 true 对应的布尔对象的相等性。并且您在代码中使用第一个条件,因为您的 Java 版本不支持自动拆箱,因此您需要使用布尔对象。

What is the difference between Boolean.TRUE and true in Java?

Java 中的 Boolean.TRUE 和 true 有什么区别?