Hi I would like to know diff between the above comparisons?
I am getting null pointer exception when I check object.getItems() == null
.
But if I change it to null == object.getItems()
, it workes fine.
I did look into this what is the difference between null != object and object!=null But I didnt get satisfactory answer.
There is absolutely no difference in either semantics or performance. The == in this case is a reference inequality operation; it can never throw NullPointerException .
Typically, you'll check for null using the triple equality operator ( === or !== ), also known as the strict equality operator, to be sure that the value in question is definitely not null: object !== null . That code checks that the variable object does not have the value null .
To check if it is null, we call the isNull() method and pass the object getUserObject as a parameter. It returns true as the passed object is null.
In object-oriented computer programming, a null object is an object with no referenced value or with defined neutral ("null") behavior. The null object design pattern describes the uses of such objects and their behavior (or lack thereof).
(Similar question: Which is more effective: if (null == variable) or if (variable == null)?)
Difference between null==object and object==null
There is no semantical difference.
object.getItems() == null
and null == object.getItems()
are equivalent.
Perhaps you're mixing it up with the fact that
nonNullObj.equals(obj)
and
obj.equals(nonNullObj)
can make a difference (since the second alternative could result in a NPE in case the callee is null).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With