[英]How to check whether an Integer is null or zero in Java?
Is there more concise way to write:有没有更简洁的写法:
if (myInteger != null && myInteger != 0) { ... }
For example, for Strings you can use StringUtils.isBlank()
例如,对于字符串,您可以使用
StringUtils.isBlank()
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 条件,具体取决于您的用例...
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)) { ... }
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 ;)这是不可读的,我同意;)
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.我希望这可以帮助你。
private boolean isNullOrZero(Integer i){
return i == null || i.intValue() == 0;
}
For some other types:对于其他一些类型:
i.longValue() == 0
for Long i.longValue() == 0
为 Long
i.doubleValue() == 0
for Double i.doubleValue() == 0
为 Double
i.shortValue() == 0
for Short i.shortValue() == 0
表示 Short
i.floatValue() == 0
for Float i.floatValue() == 0
表示 Float
Depending on your implementation of myInteger
(ie 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
. Integer
是实际对象,这意味着它们可以为null
。 That being said they can also hold 0 as a value.话虽如此,它们也可以将 0 作为值。 So, in this case you would have to make both checks.
因此,在这种情况下,您必须同时进行两项检查。
int
is a primitive and cannot hold the value of null
. int
是一个原始类型,不能保存null
的值。 In this case you would only have to check if your int
property is not equal to 0.在这种情况下,您只需检查您的
int
属性是否不等于 0。
There is alwo a nullsafe way to do it like:有一种 nullsafe 方法可以做到这一点,例如:
Long val = null;
if( val == Long.valueOf( 0 ) ) {...}
or或者
if( Objects.equals( val, 0L ) ) {...}
ObjectUtils.nullSafeEquals(0, myInteger)
would be good to use if you don't want to increase cyclomatic complexity AND still get the functionality of myInteger.equals(0) while handling null values appropriatly.如果您不想增加圈复杂度并且仍然获得 myInteger.equals(0) 的功能,同时适当地处理空值,那么
ObjectUtils.nullSafeEquals(0, myInteger)
会很好用。
private boolean isNotNullAndZero(Long num){
return Optional.ofNullable(num).orElse(0L) != 0L ? true:false;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.