Ads Top

Java Wrapper Classes


Java provides wrapper classes for each of their primitive types. In this blog post, we are going to discuss Java wrapper classes and their corresponding primitive type. We will also discuss how automatic boxing and unboxing happens between Java wrapper types and primitive types.

Table of Contents:

  1. Java Wrapper Classes
  2. Wrapper Classes and their corresponding Primitive types
  3. Why we need Wrapper Class in Java
  4. Autoboxing and Unboxing

Java Wrapper Classes:

Wrapper classes, as the name suggests, wraps the primitive type. Many classes and data structures in Java work with object types and not primitives. To overcome this issue, Java provides a wrapper class for each primitive type. A wrapper object stores a value of its corresponding primitive type.

Wrapper Classes and their corresponding Primitive types:

The below table shows the wrapper classes and their corresponding primitive type.

Wrapper Class Primitive Type
Boolean boolean
Character char
Byte byte
Short short
Integer int
Long long
Float float
Double double

Below is the code snippet which shows how to create a wrapper object for an integer type.
 Integer object = new Integer(12);  

Or we can also create a wrapper object as shown below:
 Integer object = 12;  

Similarly, we can create wrapper objects for other primitive types:
 Boolean myBoolean = new Boolean(true);  
 Character myChar = new Character('D');  
 Byte myByte = new Byte((byte) 23);  
 Short myShort = new Short((short)99);  
 Long myLong = new Long(1004L);  
 Float myFloat = new Float(4.567F);  
 Double myDouble = new Double(5.78);  

Why we need Wrapper Class in Java

Some Java libraries works only with objects. For example, the collection framework in Java works only with objects. As the collection framework uses generics and a restriction of generics in Java is that it can not use primitives for its type parameter.

For example, the following below code will not work:
 List<int> intList = new ArrayList<>();  
The above code will work only if we use Integer wrapper class instead of int(Primitive type) 
 List<Integer> intList = new ArrayList<>();  
Before Java 5, a data type had to convert into an object before adding to a stack or vector. For this, wrapper classes were introduced.

Also, you need an object, in the context, when you want to manipulate the object that is being passed to a parameter of a method, as in case of primitive a copy of the variable is passed not a reference.

Wrapper object also allows you to store null value, whereas in primitive, you can not store null value.

Autoboxing and Unboxing


Autoboxing means automatic conversion between primitive data type and their equivalent wrapper object. This process takes place completely in the compiler. For example, converting a long to Long, an integer to Integer, etc.

Let's see a few examples when Autoboxing occurs.

Example 1: When we pass a primitive value to a method that is expecting a wrapper type object. For instance, in the below code, we pass a primitive integer value 4 to the method named squareRoot, which expects an integer type wrapper object. But still, code compiles with no errors and gives the output 16. This is because Java compiler automatically boxes the int, with an implicit call to new Integer(4).
 public class AutoBoxing {  
   public static void main(String[] args) {  
     Integer result = squareRoot(4);  
   public static Integer squareRoot(Integer number){  
     return number * number;  
Output: 16

Example 2: When we assign a primitive value to a Wrapper object, here also compiler does the autoboxing for us.
 Integer myInt = 5;  
 Long myLong = 67L;  

Unboxing: It is just the reverse process of Autoboxing, in which a wrapper type object is converted to its corresponding primitive type. For example, conversion of an Integer to int.

Example 1: When we pass a wrapper type object to a method expecting primitive type. For example, in the below code snippet we have passed an integer wrapper type object to a method named multiplyByFour. Behind the scene, the Java compiler will call the number.intValue( ).
 public class AutoBoxing {  
   public static void main(String[] args) {  
     Integer number = new Integer(10);  
     Integer result = multiplyByFour(number);  
   public static Integer multiplyByFour(int number){  
     return number * 4;  

Example 2: When assigning a wrapper type object to a primitive type.
 Integer myInt = new Integer(58);  
 int num = myInt;   
Here also, Java will call myInt.intValue() implicitly.


In this article, we have discussed what are wrapper types and their equivalent primitive types. Then we discussed why we need wrapper objects and finally, we also looked into Autoboxing and Unboxing.

No comments:

Powered by Blogger.