如何在 Java 中检查整数是 null 还是零?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41183668/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check whether an Integer is null or zero in Java?
提问by Mykhaylo Adamovych
Is there more concise way to write:
有没有更简洁的写法:
if (myInteger != null && myInteger != 0) { ... }
For example, for Strings you can use StringUtils.isBlank()
例如,对于字符串,您可以使用 StringUtils.isBlank()
回答by CraigR8806
Depending on your implementation of myInteger
(i.e. if the Integer property within myInteger
is the box type Integer
or the unboxed primitive int
), you may only have to write one conditional.
根据您的实现myInteger
(即,如果其中的 Integer 属性myInteger
是框类型Integer
或未装箱的原语int
),您可能只需要编写一个条件。
Integer
s are actual objects, which means they have the ability to be null
. That being said they can also hold 0 as a value. So, in this case you would have to make both checks.
Integer
s 是实际对象,这意味着它们具有成为 的能力null
。话虽如此,它们也可以将 0 作为值。因此,在这种情况下,您必须同时进行两项检查。
int
is a primitive and cannot hold the value of null
. In this case you would only have to check if your int
property is not equal to 0.
int
是一个原始类型,不能保存 的值null
。在这种情况下,您只需检查您的int
属性是否不等于 0。
回答by AxelH
I would use a ternary condition for this. Something like :
我会为此使用三元条件。就像是 :
public static boolean isNullorZero(Integer i){
return 0 == ( i == null ? 0 : i);
}
This is not readable, I agree ;)
这是不可读的,我同意;)
回答by Florian Albrecht
With Java 8:
使用 Java 8:
if (Optional.ofNullable(myInteger).orElse(0) != 0) {
...
}
Note that Optional
may help you to completely avoid the if condition at all, depending on your use case...
请注意,这Optional
可能会帮助您完全避免 if 条件,具体取决于您的用例...
回答by Yoory N.
Since StringUtils
class is mentioned in the question, I assume that Apache Commons lib is already used in the project.
由于StringUtils
问题中提到了类,我假设项目中已经使用了 Apache Commons lib。
Then you can use the following:
然后您可以使用以下内容:
if (0 != ObjectUtils.defaultIfNull(myInteger, 0)) { ... }
Or using static import:
或者使用静态导入:
if (0 != defaultIfNull(myInteger, 0)) { ... }
回答by Marcos Echagüe
I created a helper method that maybe can help you, it uses reflection so you have to think if is necessary to use it, also you need java 8.
我创建了一个可能可以帮助你的辅助方法,它使用反射,所以你必须考虑是否有必要使用它,你还需要 java 8。
The method is for all java numbers:
该方法适用于所有 java 数字:
public class NumberUtils {
private NumberUtils(){
}
public static < T extends Number> T getNumberOrZero(T number, Class<? extends Number> clazz) {
return Optional.ofNullable(number)
.orElse(getZeroValue(clazz));
}
@SuppressWarnings("unchecked")
private static < T extends Number> T getZeroValue( Class<? extends Number> clazz){
try{
Constructor<? extends Number> constructor = clazz.getDeclaredConstructor(String.class);
return (T) constructor.newInstance("0");
}catch (ReflectiveOperationException e) {
throw new IllegalArgumentException("Can't get zero value ", e);
}
}
}
You can call in this way:
你可以这样调用:
Integer myNumber = NumberUtils.getNumberOrZero(someIntegerThatCanBeNull, Integer.class);
I hope this can help you.
我希望这可以帮助你。