如何在 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:13:08  来源:igfitidea点击:

How to check whether an Integer is null or zero in Java?

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 myIntegeris the box type Integeror the unboxed primitive int), you may only have to write one conditional.

根据您的实现myInteger(即,如果其中的 Integer 属性myInteger是框类型Integer或未装箱的原语int),您可能只需要编写一个条件。

Integers 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.

Integers 是实际对象,这意味着它们具有成为 的能力null。话虽如此,它们也可以将 0 作为值。因此,在这种情况下,您必须同时进行两项检查。

intis a primitive and cannot hold the value of null. In this case you would only have to check if your intproperty 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 Optionalmay help you to completely avoid the if condition at all, depending on your use case...

请注意,这Optional可能会帮助您完全避免 if 条件,具体取决于您的用例...

回答by Yoory N.

Since StringUtilsclass 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.

我希望这可以帮助你。