Introduction to basic types of packaging classes
In general, we often use meta-types in our programs, such as
int data = 1;
float data = 2.1F;
However, in some scenarios it is not possible to use meta-types directly, such as if we want to create an int type of ArrayList, you cannot write directly:
Because the type requirement in the ArrayList "<>" symbol is a class, it cannot be a meta-type.
So Java in order to solve this problem, put forward the packaging class solution. For each meta-type, Java introduces a wrapper class (wrapper class) to hold the value of the meta-type. Now Java is able to automatically package the meta-types in your program. This means that where you want to use the wrapper class, even if your code is written in meta-type, Java will help you wrap it automatically. In addition, if you are using a wrapper class where you need to use the meta type, Java will automatically unpack it.
is a wrapper class that corresponds to several types of numeric elements:
As we can see, the following six types of numeric wrapper classes are inherited from a parent class, number.
Common methods of packing class
The wrapper class contains many commonly used methods, which are grouped into two categories. One is the method inherited from number, and the other is the unique method of this class.
The method that inherits from number
1. Remove the package class corresponding to the value of the series method, the return of the method is the wrapper class object corresponding to the truth of the meta-type.
2. Two comparison functions of the same kind
3. Determine if the values of two objects of the same class are equal
The wrapper class added itself
Each class defines itself as a unique method for some special purposes, and we use an integer class example, and the other class method is similar to the integer, and is not described.
Character Packing class
There is also a type in the basic type, Java also provides it with wrapper class, that is char, wrapper class character.
There are several more commonly used methods in the character class, as follows:
Java Fundamentals point 1: Basic type wrapper class