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

2019-02-21 21:37发布

This question already has an answer here:

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条回答
我只想做你的唯一
2楼-- · 2019-02-21 22:09

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

查看更多
登录 后发表回答