
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,
1 |
String test = “hi”; |
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.
1 2 |
test.charAt(0); test.length(); |
are some examples of the methods associated with the object test.

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.
1 2 |
int test2 = 10; test2. //Nothing appears |
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 type | wrapper name |
---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
boolean | Boolean |
char | Character |
suppose, I want to create an Integer object.
1 |
Integer testInt = new Integer(10); |
will create an Integer variable testInt and assign a reference to an Integer object with value 10.

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()
1 |
testInt = Integer.valueOf(20); |
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
1 |
testInt = 20; |
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!
Un-boxing
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.
eg:
1 2 |
Integer testInt = 20; //auto-boxing is done! int i = testInt; //un-boxing is done! |
2. When you pass a wrapper object as a parameter to a method which needs the corresponding primitive type,
eg:
1 2 3 |
public static void testMethod(int i){ System.out.println(i); } |
in the main() method
1 2 |
Integer testInt = 20; //auto-boxing is done! testMethod(testInt); //un-boxing is done! |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
Integer testInt = 20; String str = "Hi"; ArrayList test = new ArrayList(); //Creates a new ArrayList object //referred by the variable test test.add(str); //Adds a String type value to test //the index of this str is 0. test.add("Hi"); //index of this String in the //ArrayList is 1 test.add(testInt); //Adding a Integer value to test //Index is 2 test.add(20); //auto-boxing is done! Index is 3 System.out.print((String) test.get(0)); |
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 🙂