检查Integer Wrapper是否为NULL以及原始值0

Sha*_*upe 5 java autoboxing

在发布此问题之前,我已经参考了这个问题.
检查Null Wrappers是否符合原始值

我有我要检查情况Wrapper Integernull以及0

if( statusId != null || statusId != 0 ) //statusId is Integer it maybe 0 or null
    // Do Somethimg here
Run Code Online (Sandbox Code Playgroud)

我怎样才能克服这种情况?

dav*_*xxx 7

替换orand:

if( statusId != null && statusId != 0 ) 
Run Code Online (Sandbox Code Playgroud)

它会起作用,因为只有statusId不是null :

statusId != null
Run Code Online (Sandbox Code Playgroud)

你会尝试拆箱statusIdint:

statusId != 0 
Run Code Online (Sandbox Code Playgroud)

而在的情况statusId就是null,短路&&运营商将防止抛出NullPointerExceptionstatusId != 0将不予评审.

  • 更准确地说,这将起作用,因为`&&'的短路行为,右手操作数(表达式)仅在左手操作数(表达式)产生真值时才被评估.我不明白为什么人们会回答你的答案,因为这是表达支票最"自然"的方式? (3认同)