Is there any reason for “Boolean. True. Equals (x)” in Java?
•
Java
I encountered this code in one of the projects
(this is in Java)
if (Boolean.TRUE.equals(foo.isBar()))
Foo#isbar() is defined as Boolean isbar(), so it cannot return null
Is there any reason to write that? I can write it myself
if (foo.isBar())
But maybe I missed something subtle
thank you
Solution
Since isbar returns an original Boolean value, there is no semantic difference In addition, the second method is simpler, clearer and more efficient, because the result does not have to be automated for the call, and then extract the original Boolean value again Considering this, there is no reason to use the first method and several use the second method, so use the second method I leave a lot of room for my coders, but I'll sit down and chat with anyone who adds something similar to professional code
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码