JDK5.0 new Features-automatic packing/unpacking

Source: Internet
Author: User

The lJDK5.0 syntax allows the developer to assign a basic data type directly to the corresponding wrapper class variable, or to a variable of type Object, which is called automatic boxing. L Automatic unpacking and automatic boxing in contrast, the wrapper class object is assigned directly to a corresponding basic type variable. L Typical application: List list = new ArrayList (); List.add (1); int j = (Integer) list.get (0);
 PackageCn.itcast.autobox;Importjava.util.ArrayList;Importjava.util.List;Importorg.junit.Test; Public classDemo {@Test Public voiddemo1 () {//Automatic Boxing//integer in=new integer (ten);Integer in = 10; //Automatic Unpacking        inti =NewInteger (10);//Call the Intvalue () method    }    //Automatic unpacking and boxing application----collection. The elements in the collection are all object@Test Public voidDemo2 () {List<Integer> list =NewArraylist<integer>(); List.add (10);//An automatic boxing operation was performed.                intN=list.get (0); }        //the pen question about the integer@Test Public voidDmo3 () {//integer in1=new integer (+);//integer in2=new integer (+);//        //Integer in3=100; //integercache[100];//Integer in4=100;//        //System.out.println (in1==in2);//false//System.out.println (in2==in3);//false//System.out.println (IN3==IN4);//trueInteger in1=NewInteger (1000); Integer in2=NewInteger (1000); Integer in3= 1000;//new Integer (+);Integer in4=1000; System.out.println (in1==IN2);//falseSystem.out.println (in2==in3);//falseSystem.out.println (IN3==IN4);//false    }}

1. Automatic box packing and unpacking.
The wrapper class in Java.
Wrapper classes are wrapping basic data in Java, and you can wrap a base type into a class type.

Basic data types

Four categories of eight.
1. Integer byte short int long
2. Float type float double
3. Character Char
4. Boolean Boolean

Packing class
Byte Short Integer Long
Float Double
Character
Boolean

Automatic unpacking
Assigning an Integer object directly to an int

Automatic Boxing
Assigns an int directly to an integer.


For an integer class that is automatically boxed, if the int value is between -128----127 A value that is taken out of the Integercache,
If not within this range is the new Integer ()

JDK5.0 new Features-automatic packing/unpacking

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.