gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 01:31:47 25 4
gpt4 key购买 nike

在发布这个问题之前我已经引用了这个。
Checking Null Wrappers against primitive values

我的情况是我想用 null 检查 Wrapper Integer 以及 0

if( statusId != null || statusId != 0 ) //statusId is Integer it maybe 0 or null
// Do Somethimg here

我该如何克服这种情况?

最佳答案

or替换为and:

if( statusId != null && statusId != 0 ) 

只有当 statusId 不是 null 时才会起作用:

statusId != null

你将尝试将 statusId 拆箱为 int:

statusId != 0 

并且在 statusIdnull 的情况下,短路 && 运算符将防止抛出 NullPointerException 作为 statusId != 0 将不会被评估。

关于java - 检查 Integer Wrapper 是否为 NULL 以及原始值 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45278904/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com