What is automatic and automatic packing unpacking

What is automatic and automatic packing unpacking

There are eight basic types in Java, it can be divided into four categories:
shaping:
byte (one byte to store, in the range of 2 ^ 7-1 7- ^ -2),
Short (two bytes to store, in the range - 15-1 15- 2 ^ 2 ^),
int (four bytes of storage, 31- 2 ^ -2 ^ 31-1),
Long (eight bytes of storage, in the range -2 ^ 63-2 ^ 63-1)
float: float, double
char: char
boolean type: boolean

Note:
When performing the same type of calculation may be out of range overflow. Overflow not reported abnormal nor any hints. Therefore, the results of performing the same type of calculation formula must pay attention to the calculation will not produce overflow.


type of packaging:

java is an object-oriented language, but is indeed basic types java object is not oriented, to solve this problem in the design of classes, each basic types are designed for a class corresponding to said eight basic types of such phase and corresponding class called packaging (wrapp calss).


Located java.lang package packaging, packaging and basic types of correspondence is as follows:

Basic data types Wrapper class
mean Mean
short Shoty
int Integer
long Long
float Float
double Double
char Character
boolean Boolean

Why do we need packaging:

Because java object to be oriented so that it creates a wrapper class of objects have characteristics ,, and their corresponding add properties and methods to enrich the basic type of operation,


Unpacking and packing:

With basic types and package types, will certainly be among the conversion.
We believe that the package is the basic type of packaging, it is converted to the basic type of process is called type packaging containers (Boxing);
conversely converting the basic type of packaging process is called unpacking (Unboxing);

Is manual work before the JAVA SE5 packing can be achieved by the following code:

Integer i=new Integer(1);

Automatic and automatic packing unpacking
JAVASE5 in order to solve this problem, java provides auto-boxing and auto-unboxing function.
Autoboxing: basic data type is automatically converted to the corresponding packaging;
auto-unboxing: the packaging is automatically converted to the basic data types:

Integer i=1;//自动装箱

int   a=i;	//自动拆箱

Integer i = 1; may be replaced Integer i = new Integer (1), the object is to provide an automatic packing function does not require a developer to manually new Integer of;

note:

Automatically unpacking will also have some problems, when the value of the object to be packaged are compared, can not simply use == to judge, when the value should be the object when -128-127 can be compared between the out of range compared using equals;

		Integer a=2;
        Integer b=2;
        System.out.println(a==b);
        System.out.println(a.equals(b));
        
 输出结果为:
        true
		true
		Integer a=200;
        Integer b=200;
        System.out.println(a==b);
        System.out.println(a.equals(b));
 输出结果为:
        false
		true

Published 10 original articles · won praise 0 · Views 121

Guess you like

Origin blog.csdn.net/DONG__CHI/article/details/103689729