简体   繁体   中英

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)

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

Foo#isBar() is defined as boolean isBar() , so it can't return 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. In that case you can always write if (foo.isBar()) . If you foo.isBar() returns Boolean then it can be either Boolean.TRUE , Boolean.FALSE or 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.

Over and above if (foo.isBar()) will fail, when foo.isBar() returns Boolean NULL.

Since isBar returns 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.

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 ..)

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 .


The only reason I am aware of is if foo.isBar was typed as returning Boolean (not boolean ) and where it may return 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)

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 .

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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