简体   繁体   English

比较整数对象与原始int

[英]comparison Integer object with primitive int

I am comparing integer object with primitive int but getting null pointer exception if Integer object is null 我将整数对象与原始int进行比较,但如果Integer对象为null则获取空指针异常

public static void main(String[] args) {

        Integer a = null;
        int b = 1;

        if (a != b) {
            System.out.println("True");
        }

    }


Output : Exception in thread "main" java.lang.NullPointerException
    at com.nfdil.loyalty.common.Test.main(Test.java:10)

I am getting this because its trying to convert null integer object (a.intValue()) into primitive int . 我得到这个是因为它试图将null integer object (a.intValue()) into primitive int

Is there any other way to avoid this? 还有其他方法可以避免这种情况吗?

您可以使用Objects.equals

if (!Objects.equals(a, b)) { ... }

For the sake of being able to compare the two, you can avoid this exception by casting b to an Integer . 为了能够比较两者,您可以通过将b转换为Integer来避免此异常。

Integer a = null;
int b = 0;
System.out.println(a != (Integer) b);

This will always result in true however, since a and (Integer) b evaluate each evaluate to different objects. 然而,这将始终为true ,因为a(Integer) b每个求值评估为不同的对象。

The reason that your original code threw an exception is because the JVM tried to convert a to an int . 原始代码抛出异常的原因是因为JVM试图将a转换为int a had a value of null however, and the JVM can't convert that to an int . a有值null然而,和JVM无法转换成int Casting the int to an Integer beforehand ensures that both of the compared terms are Integer objects so they can be compared normally, without the JVM having to do any unsafe implicit casting. 事先将int转换为Integer可确保两个比较的术语都是Integer对象,因此可以正常比较它们,而JVM不必进行任何不安全的隐式转换。

If you want to compare the numerical value of the two items, you could use an approach such as this: 如果要比较两个项目的数值,可以使用如下方法:

Integer a = null;
int b = 0;
System.out.println(!((Integer) b).equals(a));

您可以先将b转换为Integer ,然后与.equals进行比较:

Integer.valueOf(b).equals(a)

Use if (a != null && a == b) to test of objects should be considered equal, or if (a == null || a != b) to test of objects should be considered unequal. 使用if (a != null && a == b)来测试对象应该被认为是相等的,或者if (a == null || a != b)对象的测试应该被认为是不相等的。 Since b is an int rather than Integer it can't be null. 由于bint而不是Integer ,因此不能为null。 Thus, if a is null it can't match b . 因此,如果a为null,则它不能与b匹配。

While one could convert b to an Integer and then use Object.equals() upon it, a literal interpretation of such code would ask the compiler and runtime to do a lot of needless work. 虽然可以将b转换为Integer然后在其上使用Object.equals() ,但对此类代码的字面解释会要求编译器和运行时执行大量不必要的工作。 While some versions of the JVM (runtime) might be able to recognize that one of the arguments to equals will always be a non-null Integer , and that it can thus skip a lot of the needless work and instead generate code equivalent to the if statement above, there's no guarantee that all versions would do so (and in fact, I would be very surprised if there weren't some versions that can't). 虽然某些版本的JVM(运行时)可能能够识别出equals一个参数将始终是一个非空的Integer ,并且因此它可以跳过许多不必要的工作,而是生成相当于if代码。上面的陈述,不能保证所有版本都会这样做(事实上,如果没有一些版本不能,我会非常惊讶)。

Incidentally, this approach will test the numerical value of a against b even if b happens to be numeric primitive type other than int [eg short , long or double ]. 顺便说一句,这种方法将测试ab的数值,即使b碰巧是除int之外的数字原始类型[例如shortlongdouble ]。 Behavior when b is float might be a little unexpected [it compares b not with the number in a , but with the float value nearest to a ], but when using long or double it will test for an exact numerical match. 行为当bfloat可能有点意外[它比较b不与数a ,但与float最接近值a ],但使用时longdouble它将测试一个确切的数值匹配。 By contrast, approaches using equals would report that a was not equal to any number of any type other than int or Integer . 相比之下,使用equals方法会报告a不等于除intInteger之外的任何类型的任何数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM