First of all, apologies to this very basic question. I'm fairly new to stackoverflow but I'm scratching my head over this and would like some enlightenment.
Okay, so today at work me and my colleague's code was peer reviewed and was told to replace this..
Boolean isOpen;
*some processing that involves altering the value of isOpen*
if (!isOpen){
...
}
with this..
if (Objects.equals(Boolean.FALSE, isOpen)){
...
}
I get it that both implementation is correct and produces the same result. I also know the consequences of using the Boolean
object wrappper.
What I don't get it the difference between using the NOT operator and Objects.equals() in this context. I know that at runtime the isOpen
will be unboxed. Both are still susceptible to a NullPointerException
so what's the difference? It kinda beats the purpose of logical operators.
There's no static
equals
method in Object
class.
You probably meant Objects.equals()
. The advantage of using Objects.equals(Boolean.FALSE, isOpen)
instead of !isOpen
is to avoid NullPointerException
when isOpen
is null
.
That said, I'd rather change isOpen
from Boolean
to boolean
if possible, and keep the !isOpen
condition.