What are Wrapper Classes in Java

Image Credit - Pixabay

Wrapper Classes are Classes that have written to make objects from the primitive types in Java. They are used to “wrap” the primitive values in an object. We have learnt in inheritance article  that the Class called “Object” is the supreme parent Class for any object such as String. But, primitive types in Java are not children of Object. So they are not objects as well. There are instances where we have to make objects from a primitive value. I’ll give an example of such an instance later in this article. At that point, we have to use Wrappers.

We know Java has 8 primitive types. Byte, short, int, long, float, double, boolean and char. All of these types are called primitive because they are NOT objects like the type String. But String, being an object type, has many operations that could be done by itself. For example,

test is a String variable which holds a reference to an object. So, we can call all the public instance methods of String through test.

are some examples of the methods associated with the object test.

Figure 01: There are a large number of String methods available for performing various tasks

But, the primitive types don’t have such kind of ability. Try creating a int variable test2 and see whether any instance method pops up.

Figure 02: No any method will pop up

Is Java Purely Object Oriented?

Java is not a PURE object oriented language. If a language to be purely Object Oriented, all the types in it should be objects. But since Java has primitive values, it is not. So wrappers have been created as an attempt to reduce this issue and make Java more powerful.

So, how to we create a wrapper Object for a primitive Class? Here it is;

There is a wrapper Class for each primitive class

primitive typewrapper name
int Integer
long Long
float Float
double Double
boolean Boolean
char Character

suppose, I want to create an Integer object.

will create an Integer variable testInt and assign a reference to an Integer object with value 10.

Figure 02: Various methods are available when we use a wrapper for a primitive type

Now try to call a method of testInt.
You will see there are a plenty of methods available for this testInt.

There are two other ways to assign a reference to the variable testInt;

1. Use the static method valueOf()

valueOf() is a static method of Integer Class which takes an int value as the input parameter and returns a reference to a Integer object representation of that int value. Here the input is 20.

2. Auto-boxing

A remarkable feature introduced in Java 5 is autoboxing. The Java compiler automatically converts a primitive type to the corresponding wrapper Object and the reference is returned when it is assigned to such wrapper variable as the example above. This is called auto-boxing.

In the example, see, we didn’t use any method to convert this primitive value 20 to a Integer object. So your computer is smart enough to convert it automatically for you!


There’s another concept called un-boxing. It is the opposite of auto-boxing. The conversion of a wrapper object to its corresponding primitive value is called un-boxing. For example, conversion of Integer object to a primitive int value. This un-boxing is also automatic. No need to worry about doing it manually 🙂
There are two occasions where auto un-boxing is done.
1. When you assign a wrapper object variable to a matching primitive type variable.


2. When you pass a wrapper object as a parameter to a method which needs the corresponding primitive type,


in the main() method

An Example

Now, I would like to recall the importance of wrappers. By using wrappers, you can do your programming in a more Object Oriented style. You can call various methods in the wrapper object and manipulate them in a better way. So rewriting these methods from a scratch for primitive types is avoided.
There’s another very important use of wrapper Classes.
Java has a feature to store a collection of objects in a single variable. Note that only Objects are allowed to store in the collection.

Eg: ArrayList is a type which can store a list of any type of Object.

In the main() method:

The last sentence could be a little complicated one for a starter. First the method test.get(0) is called. This method will return a reference to the object in the 0th index position of the ArrayList test. Here, it is the object in the String variable str.

But this returned object is of type Object (as ArrayList can hold any type of Object, it will type-cast any object which adds to it, in to the type Object. So the return type of ArrayList is also the Object type) Therefore, we have to type-cast it in to (String) before passing to the print method. (this print statement would still work without the (String) type-cast, but, it is also important to know the type-casting as well 🙂

All I want to say is, without wrapper Classes, you cannot add a primitive type to a Collection object like ArrayList, because ArrayList stores the items as Object type. Since primitive types are not a child class of Object, they cannot be embedded inside ArrayList. So, wrappers save the day!

Then why Primitive types are still in Java?

Why primitive types are still available in this modern OOP world? Answer is simple. Speed! Operations done with primitive types are much faster than implementing them using their wrappers or alternative Objects.

There’s another advantage. We can use == operator to check if two primitive types have the same value. If we use == to compare two objects, what happens is the computer checks whether the two objects point to the same memory location or not. Most of the time, this is not what we want. To compare two objects, we therefore have to use the method .equals() which is a bit verbose 🙂