简体   繁体   English

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

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

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谢谢

I hope foo.isBar() returns a boolean. 我希望foo.isBar()返回一个布尔值。 In that case you can always write if (foo.isBar()) . 在这种情况下,您总是可以写if (foo.isBar()) If you foo.isBar() returns Boolean then it can be either Boolean.TRUE , Boolean.FALSE or NULL . 如果foo.isBar()返回Boolean则它可以是Boolean.TRUEBoolean.FALSENULL 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. 在这种情况下, if (Boolean.TRUE.equals(foo.isBar()))确保if块在一个场景中执行(TRUE)并在剩余2中省略。

Over and above if (foo.isBar()) will fail, when foo.isBar() returns Boolean NULL. 超过 if (foo.isBar()) 将失败,当 foo.isBar() 返回Boolean NULL时。

Since isBar returns a primitive boolean , there is no semantic difference. 由于isBar返回一个原始boolean ,因此没有语义差异。 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. 我给其他编码人员留下了很大的余地,但我会坐下来与任何向专业代码添加类似内容的人聊聊。

I would suspect "old legacy code with no good reason" - and in fact, I would contend it is worse . 我怀疑“旧遗留代码没有充分理由” - 事实上,我认为情况更糟 (I wonder how int s are compared ..) (我想知道如何比较int ..)

The code that uses TRUE.equals requires 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.isBar was typed as returning Boolean (not boolean ) and where it may return null : 我所知道的唯一原因是foo.isBar 输入为返回Boolean (不是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;

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? 也许这是一个类似的案例?

Did I find this practical example, can be useful to someone:我是否发现这个实际示例对某人有用:

When boxed type java.lang.Boolean is used as an expression it will throw NullPointerException if the value is null as defined in Java Language Specification §5.1.8 Unboxing Conversion .当装箱类型 java.lang.Boolean 用作表达式时,如果该值为Java Language Specification §5.1.8 Unboxing Conversion 中定义的null ,它将抛出NullPointerException

It is safer to avoid such conversion altogether and handle the null value explicitly.完全避免这种转换并显式处理空值会更安全。

Noncompliant Code Example不合规的代码示例

Boolean b = getBoolean();
if (b) {  // Noncompliant, it will throw NPE when b == null
  foo();
} else {
  bar();
}

Compliant Solution合规解决方案

Boolean b = getBoolean();
if (Boolean.TRUE.equals(b)) {
  foo();
} else {
  bar();  // will be invoked for both b == false and b == null
}

in the first condition you are checking for the equality of Boolean object corresponding to true. 在第一个条件中,您正在检查对应于true的Boolean对象是否相等。 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. 并且您正在使用代码中的第一个条件,因为您的Java版本不支持自动装箱,因此您需要使用布尔对象。

What is the difference between Boolean.TRUE and true in Java? Java中的Boolean.TRUE和true有什么区别?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM