In JAVA programming, a series of types are often used, such as int char double. You can think of them as "Basic" types.
They obviously need special treatment, because new puts the object in a "heap", but simple variables such as byte and char are not very efficient in the heap.
For basic type processing, java adopts the same method as C and C ++. That is, you do not need to use new to create a variable. Instead, you can create a variable that is not an automatic variable referenced. This variable directly stores "values" in the stack, so it is more efficient.
However, because the basic types are not derived from JAVA objects, many operations cannot be completed.
For example, the mutual conversion between basic data types can not be brake conversion, but the forced conversion will cause problems, for example, if the String type is converted to the int type, jdk provides the corresponding packaging class for the convenience of users.
[Java]
<Span style = "font-size: 18px;"> int a = 0;
String result = Integer. toString (a); </span>
Java packaging class is very flexible to use
[Java]
<Span style = "font-size: 18px;"> char c = 'X ';
Character ch = new Character (c); </span>
You can also use
[Java]
<Span style = "font-size: 18px;"> Character ch = new Character ('x ');
And reverse conversion:
Char c = ch; </span>
Example 2:
Second, sometimes a function needs to pass an Object variable, but you cannot pass the int type. What should I do?
[Java]
<Span style = "font-size: 18px;"> public void test (Object obj ){
} </Span>
You want to pass 5 in.
Test (new Integer (5 ));
To sum up, the packaging class has two usage: one is to convert between the basic types; the other is to facilitate the function to pass values; the other is to conveniently Replace the basic data type when objects are used in some places.