English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, we will learn about Java Wrapper classes (Wrapper) with examples.
Java wrapper classes are used to convert primitive types (int, char, float, etc.) to their corresponding objects.
8Each of the basic types has a corresponding wrapper class.
Primitive types | Wrapper classes |
---|---|
byte | Byte |
boolean | Boolean |
char | Character |
double | Double |
float | Float |
int | Integer |
long | Long |
short | Short |
We can also use the valueOf() method to convert primitive types to their corresponding objects.
class Main { public static void main(String[] args) { //Create primitive type int a = 5; double b = 5.65; //Convert to wrapper object Integer aObj = Integer.valueOf(a); Double bObj = Double.valueOf(b); if(aObj instanceof Integer) { System.out.println("Create an Integer object."); } if(bObj instanceof Double) { System.out.println("Create a Double object."); } } }
Output result
Create an Integer object. Create a Double object.
In the above example, we used the valueOf() method to convert primitive types to objects.
Here, we use the instanceof operator to check if the generated object belongs to Integer or Double type.
However, the Java compiler can directly convert primitive types to corresponding objects. For example,
int a = 5; //Convert to object Integer aObj = a; double b = 5.6; //Convert to object Double bObj = b;
This process is calledAutomatic boxing. For more information, please visitJava Auto-boxing and Un-boxing.
Note:We can also use the constructor of the wrapper class (Wrapper) to convert primitive types to wrapper objects.But in Java 9After that, no longer use the constructor.
To convert an object to a primitive type, we can use the corresponding value methods (intValue(), doubleValue(), etc.) in each wrapper class.
class Main { public static void main(String[] args) { //Create an object of a wrapper class Integer aObj = Integer.valueOf(23); Double bObj = Double.valueOf(5.55); //Convert to primitive type int a = aObj.intValue(); double b = bObj.doubleValue(); System.out.println("The value of a:") + a); System.out.println("The value of b:") + b); } }
Output result
The value of a: 23 The value of b: 5.55
In the above example, we used intValue() and doubleValue() methods to convert Integer and Double objects to their corresponding primitive types.
However, the Java compiler can automatically convert objects to their corresponding primitive types. For example,
Integer aObj = Integer.valueOf(2); //Converted to int type int a = aObj; Double bObj = Double.valueOf(5.55); //Converted to double type double b = bObj;
This process is calledUn-boxing. For more information, please visitJava Auto-boxing and Un-boxing.
In Java, sometimes we may need to use objects instead of primitive data types. For example, when using collections.
// Error ArrayList<int> list = new ArrayList<>(); //Normal operation ArrayList<Integer> list = new ArrayList<>();
In this case, the wrapper class can help us use primitive data types as objects.
We can store null values in wrapper objects. For example,
//An error will be generated int a = null; // Normal operation Integer a = null;
NoteBasic types are more efficient than their corresponding objects. Therefore, it is always recommended to use primitive types when efficiency is needed.