简体   繁体   English

Java中的整数实例和int原始值比较

[英]Integer instance and int primitive value comparison in Java

Integer a = 5;
int b = 5;

System.out.println(a==b); // Print true

But why does this print true, since a is an instance of an Integer and b is a primitive int ? 但为什么这个打印是真的,因为a是一个Integer的实例而b是一个原始的int

Java uses the concept of Unboxing when you compare primitive and wrapper class. 当您比较原始类和包装类时,Java使用Unboxing的概念。 Where in a Integer variable is translated into primitive int type. Integer变量中的哪些内容转换为原始int类型。

Following is what is happening with your code: 以下是您的代码所发生的事情:

Integer a = 5; //a of type Integer i.e. wrapper class
int b = 5; //b of primitive int type

System.out.println(a==b) // a is unboxed to int type and compared with b, hence true

For more on Autoboxing (reverse of Unboxing) and Unboxing this link. 有关Autoboxing (逆Autoboxing )和Unboxing Autoboxing 链接的更多信息。

Beginning with JDK 5, Java added two important features: autoboxing and auto-unboxing. 从JDK 5开始,Java增加了两个重要特性: 自动装箱和自动拆箱。

Autoboxing is the process by which a primitive type is automatically encapsulated (boxed) into its equivalent type wrapper whenever an object of that type is needed. 自动装箱是一种过程,只要需要该类型的对象,原始类型就会自动封装(装箱)到其等效类型包装器中。 There is no need to explicitly construct an object. 无需显式构造对象。

Auto-unboxing is the process by which the value of a boxed object is automatically extracted (unboxed) from a type wrapper when its value is needed. 自动拆箱是在需要值时从类型包装器自动提取(取消装箱)盒装对象的值的过程。

With autoboxing, it is no longer necessary to manually construct an object in order to wrap a primitive type: 使用自动装箱,不再需要手动构造对象以包装基本类型:

Integer someInt = 100; // autobox the int (i.e. 100) into an Integer

To unbox an object, simply assign that object reference to a primitive-type variable: 要取消打开对象,只需将该对象引用分配给基本类型变量:

int unboxed = someInt // auto-unbox Integer instance to an int

The correct answer is due to unboxing, but let's be more explicit here. 正确的答案是由于拆箱,但让我们在这里更明确。

The rules for numerical equivalence are described in the Java Language Specification , specifically these rules: Java语言规范中描述了数值等价的规则,特别是这些规则:

If the operands of an equality operator are both of numeric type, or one is of numeric type and the other is convertible (§5.1.8) to numeric type, binary numeric promotion is performed on the operands (§5.6.2). 如果等于运算符的操作数都是数字类型,或者一个是数字类型而另一个是可转换的(第5.1.8节)为数字类型,则对操作数执行二进制数字提升(第5.6.2节)。

Since Integer is convertible to a numeric type (per these rules ), the values you're comparing become semantically equivalent to a.intValue() == b . 由于Integer可以转换为数字类型(根据这些规则 ),因此您要比较的值在语义上等同于a.intValue() == b

It should be stressed that this conversion will fail if a is null ; 应该强调的是,如果anull ,则此转换将失败 ; that is, you will get a NullPointerException when attempting to do that equivalence check. 也就是说,在尝试进行等效性检查时,您将获得NullPointerException

Here all the post answer are correct. 这里的所有帖子答案都是正确的。

Here your code at Compile time 这是编译时的代码

public class Compare {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Integer a=5;
        int b=5;

        System.out.println(a==b);
        //the compiler converts the code to the following at runtime: 
        //So that you get it at run time
        System.out.println(a==Integer.valueOf(b));


    }

} }

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

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