A wrapper class in Java is a special class used to wrap primitive data types (such as int, double, char, etc.) into objects. The function of the wrapper class is to make the basic data type have the characteristics of the object, such as passing it as a parameter to a generic class or method, calling the method of the object, storing it in a collection, and so on.
Java provides a corresponding wrapper class for each basic data type, as shown in the following table:
basic data type | Packaging |
---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
char | Character |
boolean | Boolean |
To use a wrapper class, you first need to create an object of the wrapper class. There are two ways to create an object of the wrapper class: one is to use the constructor, and the other is to use the static method valueOf. For example, here are two ways to create an Integer object:
// 使用构造方法
Integer i1 = new Integer(10); // 创建一个值为 10 的 Integer 对象
// 使用静态方法 valueOf
Integer i2 = Integer.valueOf(20); // 创建一个值为 20 的 Integer 对象
After the object of the wrapper class is created, the methods of the wrapper class can be used to manipulate the basic data types. The wrapper class provides some commonly used methods, as shown in the following table:
method name | illustrate |
---|---|
intValue | Returns the int value corresponding to the object |
doubleValue | Returns the double value corresponding to the object |
charValue | Returns the char value corresponding to the object |
booleanValue | Returns the boolean value corresponding to the object |
compareTo | Compares the size of this object with another object, returning a negative, zero, or positive number |
equals | Determines whether this object is equal to another object |
returns a string representation of this object | |
parseXXX | Parse a string into corresponding basic data types, such as parseInt, parseDouble, etc. |
Convert a primitive data type or string to the corresponding wrapper class object |
For example, here are some examples using wrapper class methods:
// 使用 intValue 方法
int i3 = i1.intValue(); // 将 i1 对象转换成 int 值
System.out.println(i3); // 输出 10
// 使用 compareTo 方法
int i4 = i1.compareTo(i2); // 比较 i1 和 i2 的大小
System.out.println(i4); // 输出 -1
// 使用 parseDouble 方法
double d1 = Double.parseDouble("3.14"); // 将字符串 "3.14" 解析成 double 值
System.out.println(d1); // 输出 3.14
// 使用 valueOf 方法
Boolean b1 = Boolean.valueOf(true); // 将 boolean 值 true 转换成 Boolean 对象
System.out.println(b1); // 输出 true
In addition to using methods of wrapper classes, you can also use autoboxing and autounboxing to simplify your code. Auto-boxing refers to the automatic conversion of basic data types into corresponding packaging objects; automatic unboxing refers to the automatic conversion of packaging objects into corresponding basic data types. For example, here are some examples using autoboxing and autounboxing:
// 自动装箱
Integer i5 = 30; // 相当于 Integer i5 = Integer.valueOf(30);
Double d2 = 3.14; // 相当于 Double d2 = Double.valueOf(3.14);
// 自动拆箱
int i6 = i5; // 相当于 int i6 = i5.intValue();
double d3 = d2; // 相当于 double d3 = d2.doubleValue();
// 自动装箱和自动拆箱可以用在运算中