null==object 和 object==null 的区别 [英] Difference between null==object and object==null

查看:21
本文介绍了null==object 和 object==null 的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道上述比较之间的差异?

Hi I would like to know diff between the above comparisons?

当我检查 object.getItems() == null 时,我得到空指针异常.但是如果我将其更改为 null == object.getItems(),它就可以正常工作.

I am getting null pointer exception when I check object.getItems() == null. But if I change it to null == object.getItems(), it workes fine.

我确实研究过这个有什么区别在 null != object 和 object!=null 之间但我没有得到满意的答复.

I did look into this what is the difference between null != object and object!=null But I didnt get satisfactory answer.

推荐答案

(类似问题:哪个更有效:if (null == variable) or if (variable == null)?)

null==object 和 object==null 的区别

Difference between null==object and object==null

没有语义差异.

object.getItems() == nullnull == object.getItems() 是等价的.

也许你把它与

nonNullObj.equals(obj)

obj.equals(nonNullObj)

可以有所作为(因为如果被调用者为空,第二种选择可能会导致 NPE).

can make a difference (since the second alternative could result in a NPE in case the callee is null).

这篇关于null==object 和 object==null 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆