(This article is not an original article, excerpt, forget the source, sorry)
1.Java Data Types before we introduce the automatic boxing and unpacking of Java, let's look at the basic data types of java. in Java, data types can be divided into two major types, Primitive type (base type) and reference type (reference type). The value of the base type is not an object, and methods such as ToString (), Hashcode (), GetClass (), Equals () of the object cannot be called. So Java provides a wrapper type for each of the basic types. As follows:
index |
basic type |
size |
numeric range |
default value |
wrapper type |
1 |
Boolean |
--- |
True,false |
False |
Boolean |
2 |
Byte |
8bit |
-2^7--2^7-1 |
0 |
Byte |
3 |
Char |
16bit |
\u0000-\UFFFF |
\u0000 |
Character |
4 |
Short |
16bit |
-2^15--2^15-1 |
0 |
Short |
5 |
Int |
32bit |
-2^31--2^31-1 |
0 |
Integer |
6 |
Long |
64bit |
-2^63--2^63-1 |
0 |
Long |
7 |
Float |
32bit |
IEEE 754 |
0.0f |
Float |
8 |
Double |
64bit |
IEEE 754 |
0.0d |
Double |
9 |
void |
--- |
--- |
--- |
Void |
2.Java Automatic packing and unpacking definitionthe automatic boxing and unpacking mechanism is introduced in Java 1.5: (1) Auto-boxing: The basic types are wrapped with their corresponding reference types, so that they have the characteristics of the object, you can call ToString (), Hashcode (), GetClass (), Equals () and other methods. as follows:Integer a=3; //This is auto-boxing in fact, the compiler calls the static integer valueOf (int i) method, valueOf (int i) returns an Integer object representing the specified int value, then it becomes this:Integer a=3; = = Integer a=integer.valueof (3); (2) Unpacking:In contrast to automatic boxing, objects of reference types such as integers and double are re-simplified to the basic type of data. as follows:int i = new Integer (2); //This is unpacking
Java Automatic unpacking and boxing (basic data types and reference types)