/*jdk5.0 new features: The following features are appropriate for the JDK1.5 version. All versions including 1.5jdk1.4, including 1.4, cannot use the following features. Auto-boxing (auto_boxing) and auto-unpacking (auto_unboxing) */public class integertest05{public static void Main (String[] args) {//jdk5.0 before .//int-->integer (boxed) Integer i1 = new integer ( ;//integer-->int (unboxing) int i2 = i1.intvalue ();//jdk5.0, including 5.0integer i3 = 10; //Automatic packing int i4 = i3; //automatic unpacking System.out.println (i3); //"10" System.out.println (i4+1); //11//jdk5.0 M1 (321); //automatic boxing. System.out.println (M2 (10,5) + 1) //Auto Boxing}public static void m1 (Object o) { System.out.println (o);} PUBLIC STATIC INT M2 (integer i1,integer i2) {return i1 - i2; // Automatic unpacking}}//Learn more about auto boxing and unpacking/* Deep Auto Boxing and unpacking: 1. Automatic boxing and automatic unpacking is a concept in the program compilation phase, which is not related to program operation. 2. The main purpose of automatic boxing and automatic unpacking is to facilitate programmer coding. */public class integertest06{public static void main (String[] args) {Integer i1&nbSp;= new integer (+); Integer i2 = new integer (10);//There will be no automatic unpacking System.out.println ( I1==I2); //false//compares two integer data for equality and cannot use "= ="//integer has overridden the Equals method in object. SYSTEM.OUT.PRINTLN (i1.equals (I2)); //true//Focus: integer i3 = 128;integer i4 = 128;//above is equivalent to//integer i3 = new integer (+);//integer i4 = new integer (128); System.out.println (I3==I4); //false//Note the following procedure.//If the data is between ( -128~127), Java introduces an "integer constant Pool" in the method area. The integer constant pool stores only data between -128~127. integer i5 = 127; //This program does not create objects in the heap, it is taken directly from the integer constant pool. integer i6 = 127; System.out.println (I5==I6); //trueinteger i7 = -128;integer i8 = -128; System.out.println (I7==I8); //trueinteger i9 = -129;integer i10 = -129; System.out.println (i9==i10); //false}}
This article is from the "Gaogaozi" blog, make sure to keep this source http://hangtiangazi.blog.51cto.com/8584103/1662493
Auto-boxing and automatic unpacking in Java