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

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

问题描述

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



当我检查 object.getItems == null
但是如果我把它改为 null == object.getItems(),它工作得很好。



我曾经调查过这个 null != object and object!= null
但我没有得到满意的答案。

解决方案

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


null == object and object == null


没有语义上的差异。



object.getItems()== null null == object.getItems() p>

也许你正在混淆的是

  nonNullObj。 equals(obj)

  obj.equals(nonNullObj)

第二选择可以导致NPE,以防被调用者为空)。


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.

解决方案

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

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

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