Difference between null==object and object==null [

2019-02-21 21:23发布

问题:

This question already has an answer here:

  • ' … != null' or 'null != …' best performance? 15 answers

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.

回答1:

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