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