Java Container Unpacking

Source: Internet
Author: User

Reference http://how2j.cn/k/number-string/number-string-wrap/22.html

Encapsulation class

All basic types have corresponding class types
For example, an int corresponds to a class that is an integer
This class is called the encapsulation class .

 Public class Testnumber {     publicstaticvoid  main (string[] args) {        int  5;                  // converts a variable of a primitive type to an integer object        New Integer (i);         // converts an integer object to an int        of a primitive type int i2 = it.intvalue ();}             }
Number class

The digital package class has
Byte,short,integer,long,float,double
These classes are subclasses of abstract class number

 Public class Testnumber {     publicstaticvoid  main (string[] args) {        int  5;                  New Integer (i);         // Integer is a subclass of number, so printing true        System.  out . println (it instanceof number);}    }
Basic type to package class
 Public class Testnumber {     publicstaticvoid  main (string[] args) {        int  5;          // Basic type conversion to package type        New Integer (i);             }}
Encapsulation class to basic type
 Public class Testnumber {     publicstaticvoid  main (string[] args) {        int  5;          // Basic type conversion to package type        New Integer (i);                  // package type converted to base type        int i2 = it.intvalue ();}             }
Automatic Boxing

Do not need to call the constructor method, by = symbol automatic converts a base type to a class type called boxing

 Public class Testnumber {     publicstaticvoid  main (string[] args) {        int  5;          // Basic type conversion to package type        New Integer (i);                  // automatic conversion is called boxing        Integer it2 = i;             }}
Automatic unpacking

You do not need to call the Intvalue method of the integer, which is automatically converted to the int type by =, it is called the unboxing

Package Digit;  Public classTestnumber { Public Static voidMain (string[] args) {inti =5; Integer it=NewInteger (i); //package type converted to base type        intI2 =It.intvalue (); //automatic conversion is called unpacking .        inti3 =it; }}
The maximum value of int, minimum value

The maximum value of int can be obtained by its corresponding encapsulation class Integer.max_value

Should be Auto boxing must be one by one matched .

But unpacking, as long as the basic type of disassembly, its scope is less than the basic type of the left to the value range, you can.

Like what:

5555l; Long x;x= b;x=s;x=i;x

The right side is the class type, and the X on the left is the long base type, which can also be automatically disassembled .

Take X=b as an example, when B automatically unpacking the byte base type, byte value range is less than long, then there will be no compilation error.

Java Container 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.