It is assumed that you have an understanding of the basic types of Java and the automatic unpacking and boxing between the corresponding wrappers. However, it is emphasized here that automatic unboxing and boxing are implemented by the compiler inserting the appropriate method call when generating the bytecode of the class. That is, we can decompile the class file and see what appropriate method the compiler has inserted to automate boxing and unpacking. Take a look at an example:
public class Test {public static void main (string[] args) {int i1 = 1;int I2 = 1;integer integer1 = new Integer (1); integer Integer2 = new Integer (1); System.out.println (I1 = = i2);//true//Integer1 unpacking System.out.println (i1 = integer1);//true//I boxing System.out.println ( Integer1.equals (I1));//true//Two object addresses are not the same System.out.println (integer1 = integer2);//false// Two object Values System.out.println (Integer1.equals (Integer2));//true//Automatic Boxing specification requires Boolean, byte, char <= 127, between// -128~ The short and int between 127 are packaged into fixed objects, i.e.//say that the same values within these ranges correspond to the same object Integer Integer3 = 10;integer integer4 = 10; System.out.println (Integer3 = = integer4); Trueinteger Integer5 = 1000;integer INTEGER6 = 1000; System.out.println (Integer5 = = INTEGER6); False}}
The result of the operation is as follows (there are also annotations in the Class):
True
True
True
False
True
True
If there is a clear result for the example above, there is no need to look down, and if there is doubt, we can look at the contents of the anti-compilation file of the class file generated by this type:
Import Java.io.printstream;public class test{public static void Main (string[] args) { int i1 = 1; int i2 = 1; Integer integer1 = new Integer (1); Integer integer2 = new Integer (1); System.out.println (I1 = = i2); System.out.println (I1 = = Integer1.intvalue ()); System.out.println (Integer1.equals (integer.valueof (I1))); System.out.println (Integer1 = = Integer2); System.out.println (Integer1.equals (Integer2)); Integer Integer3 = integer.valueof (ten); Integer integer4 = integer.valueof (ten); System.out.println (Integer3 = = integer4); Integer Integer5 = integer.valueof (+); Integer Integer6 = integer.valueof (+); System.out.println (Integer5 = = Integer6);} }
By the way, the equal method for the integer class is given:
public boolean equals (Object obj) { if (obj instanceof Integer) { //value is the original type that is wrapped by the wrapper for the values of return value = = ((in Teger). Intvalue (); } return false; }
It should be clear now.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Comparison of 22 equality between Java integer and int type variables