Auto-boxing and automatic unpacking in Java

Source: Internet
Author: User

/*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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.