1 Wrapper Class – Boxing and Unboxing
A Wrapper Class in Java wraps primitive type and provides member methods around it. We call the process of wrapping as Boxing and reverse of it as Unboxing. With the use of a Wrapper Class we can see a primitive type as an object. Java collections do not support primitive types. But it can store objects. So, to store primitive types in a collection we can wrap it using a wrapper class. After wrapping a primitive using wrapper, we can use the member methods of the wrappers which act on the primitive type. The examples are:
- Converting a double value to string.
- Converting an integer to a long type.
We can also get back the primitive type from its wrapper. This is called Unboxing. In this article, we will see the examples to get better in sight on the Boxing and Unboxing.
2. Primitive Type & Wrapper Classes
The below table shows Primitive Types and its wrapper classes:
Primitive Type | Wrapper Class |
---|---|
int | Integer |
float | Float |
double | Double |
short | Short |
byte | Byte |
char | Character |
boolean | Boolean |
The table above shows standard primitive types and Java’s wrapper for it. We can construct them from its primitive type. There two flavors of contractor available for each Wrappers. Mean, we can construct it by sending the primitive type value as parameter or we can also send string value of the primitive type. For example, we can pass an integer 10 as it is or we can pass it as string “10”.
3. Wrapper Example (Boxing & Unboxing)
Let us say we have an integer value 15. We can construct a Wrapper Class Integer by passing this integer value to its constructor. Alternatively, we can also construct it by passing the string “15” as well. The process of getting a wrapper class Integer from the literal value 15 or string value “15” is called Boxing. Have a look at the below code:
1 |
Integer intVal1 = new Integer(15); |
Here we constructed the Integer Wrapper from the primitive value 15. Now the integer 15 is wrapped inside the Integer wrapper, which we can access through the reference intVal1. When we take out the wrapped value from this Integer class, we call it as Unboxing. This can be pictorially as follows:
4. Auto-Boxing & Auto-Unboxing
Now, let us consider the below statement:
1 |
Integer intVal3 = 10; |
In the above code, we assign an integer value 10 to an Integer wrapper. This will construct the class Integer with the value of 10. We call this as auto-boxing as Java assigns the value to the integer which is in the Wrapper. Now have a look at the below code:
1 2 |
Integer intVal1 = 7; int val = intVal1; |
Above, we have the wrapper class reference towards the right-hand side of the ‘=’ operator. Now java will unwrap the Wrapper Class and sets the wrapped content to the integer variable val. This is called auto-unboxing. Means, Java pulls the value 7 from the Integer class instance intVal1 and pushed to the integer primitive val.
5. Wrapper Class Common Methods
Each wrapper class which we saw in the previous table has useful static methods. Now, we will have a look at the below table:
Method Name | Usage |
---|---|
parse | The parse methods convert the string to type XYZ in unboxed form. Ex: parseInt, parseDouble |
valueOf | The valueOf methods converts the string to type of the class in Boxed form. Ex: Double.valueOf(string) |
toString | Converts the passed in parameter to String. Ex: Integer.toString(3500) |
The above table shows the frequently used methods of the wrapper classes. Note, all the above methods are static methods. This means, you no need to create instance and you call these methods using the class name as the qualifier. Now you can watch about the boxing and unboxing in the below video with examples:
Boxing and Unboxing – Explained
Code Example [This explained in the above video]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
//Sample 01: Boxing or Wrapping Integer intVal1 = new Integer(15); Integer intVal2 = new Integer("12"); //Sample 02: UnBoxing or Unwrapping int x; x = intVal1.intValue() + intVal2.intValue(); System.out.println("x = " + x); //Sample 03: AutoBoxing int p = 3; Integer intVal3 = 7; Integer intVal4 = p; //Sample 04: AutoUnBoxing p = intVal3; int y = intVal3 + intVal4; System.out.println("y = " + y); p = intVal3; //Sample 05: Static methods of Wrapper System.out.println(Boolean.toString(true)); System.out.println(Boolean.toString(false)); System.out.println("Number of digits in 12827 : " + Long.toString(12827).length()); double d1 = Double.parseDouble("1.5") + Double.parseDouble("3.5"); double d2 = Double.valueOf("1.5") + Double.valueOf("3.5"); |
Categories: Java
Tags: Auto-Boxing, Auto-Unboxing, Boxing, Unboxing, Wrapper Class