Java Boxing and Unboxing

Java Wrapper Class & Boxing, Unboxing

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:

  1. Converting a double value to string.
  2. 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 TypeWrapper Class
intInteger
floatFloat
doubleDouble
shortShort
byteByte
charCharacter
booleanBoolean

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:

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:

Java Boxing and Unboxing
Java Boxing and Unboxing

4. Auto-Boxing & Auto-Unboxing

Now, let us consider the below statement:

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:

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 NameUsage
parse(String)The parse methods convert the string to type XYZ in unboxed form. Ex: parseInt, parseDouble
valueOfThe valueOf methods converts the string to type of the class in Boxed form. Ex: Double.valueOf(string)
toStringConverts 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]

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.