避免!=空语句

淺唱寂寞╮ 提交于 2020-03-24 09:17:32

3 月,跳不动了?>>>

问题:

I use object != null a lot to avoid NullPointerException . 我经常使用object != null来避免NullPointerException

Is there a good alternative to this? 有没有好的替代方法?

For example: 例如:

if (someobject != null) {
    someobject.doCalc();
}

This avoids a NullPointerException , when it is unknown if the object is null or not. 如果不知道对象是否为null ,则可以避免NullPointerException

Note that the accepted answer may be out of date, see https://stackoverflow.com/a/2386013/12943 for a more recent approach. 请注意,接受的答案可能已过期,请参阅https://stackoverflow.com/a/2386013/12943以获取最新的方法。


解决方案:

参考一: https://stackoom.com/question/18dS/避免-空语句
参考二: https://oldbug.net/q/18dS/Avoiding-null-statements
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!