Wrapper Classes

On Social Media

As the name says, a wrapper class wraps (encloses) around a data type and gives it an object appearance. Wherever, the data type is required as an object, this object can be used. Wrapper classes include methods to unwrap the object and give back the data type. It can be compared with a chocolate. The manufacturer wraps the chocolate with some foil or paper to prevent from pollution. The user takes the chocolate, removes and throws the wrapper and eats it.

The primitive data types are not objects; they do not belong to any class; they are defined in the language itself. Sometimes, it is required to convert data types into objects in Java language.  A data type is to be converted into an object and then added to a Stack or Vector etc. For this conversion, the designers introduced wrapper classes.

Observe the following conversion.

The int data type i is converted into an object, i1 using Integer class. The i1 object can be used in Java programming wherever i is required an object.

The following code can be used to unwrap (getting back int from Integer object) the object i1.

intValue() is a method of Integer class that returns an int data type.

Importance of Wrapper classes

There are mainly two uses with wrapper classes.

1.       To convert simple data types into objects, that is, to give object form to a data type; here constructors are used.

2.       To convert strings into data types (known as parsing operations), here methods of type parseXXX() are used.

Normally, when we work with Numbers, we use primitive data types such as byte, int, long, double, etc.

Example:

However, in development, we come across situations where we need to use objects instead of primitive data types. In order to achieve this, Java provides wrapper classes. All the wrapper classes (Integer, Long, Byte, Double, Float, Short) are subclasses of the abstract class Number.

The object of the wrapper class contains or wraps its respective primitive data type. Converting primitive data types into object is called boxing, and this is taken care by the compiler. Therefore, while using a wrapper class you just need to pass the value of the primitive data type to the constructor of the Wrapper class.

And the Wrapper object will be converted back to a primitive data type, and this process is called unboxing. The Number class is part of the java.lang package.

Autoboxing and Unboxing

1. Autoboxing

The automatic conversion of primitive types to the object of their corresponding wrapper classes is known as autoboxing. For example – conversion of int to Integer, long to Long, double to Double, etc. 

2. Unboxing

It is just the reverse process of autoboxing. Automatically converting an object of a wrapper class to its corresponding primitive type is known as unboxing. For example – conversion of Integer to int, Long to long, Double to double, etc. 

Following is an example of boxing and unboxing −


On Social Media

Leave a Reply

Your email address will not be published. Required fields are marked *