8

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.

Community
  • 1
  • 1
priyank
  • 4,634
  • 11
  • 45
  • 52

1 Answers1

15

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

Community
  • 1
  • 1
aioobe
  • 413,195
  • 112
  • 811
  • 826