Home » Java Tutorial » Wrapper Class in Java with Examples

Wrapper Class in Java with Examples

We discovered that object-oriented programming design is really about entities in the OOPs definition guidance. The 8 – primitive byte, short, int, long, float, double, char, and boolean types of data aren’t entities. Wrapper classes are being utilized to transform primitive data types into objects, such as int to Integer, etc. Now let us consider a small instance to realize why we might choose wrapper class in java.

For instance, when dealing with Java classes, we utilize generics such as this: ArrayList<int> rather than this ArrayList<Integer> for type protection. A wrapper class in java of the primitive form is just the Integer. In this scenario, we are using wrapper class since generics require entities, not primitives. There are many factors why we use wrapper class in java rather than just a primitive type.

The table below represents the wrapper as well as its primitive types of data respectively:


Utilization of wrapper class in java

A question might arise that “why we use wrapper class in java?” The answer to this arose question is mentioned below:

  • Value change in Procedure: Java only facilitates calling by value. Thus, whenever a primitive value is transferred, the original value would not adjust. And, when we transform the primitive value of an entity, the original value will adjust.
  • The serialization: To conduct serialization, we now have to transform entities into flows. When we have a primitive value, it can be transformed via the wrapper classes into entities.
  • Java.util module: The module is named java.util offers classes of facilities to manage entities.
  • System for Collection: The Java collection system work just with entities. All collection structure classes like an array, linked lists, hash sets, vectors, tree sets, linked hash sets, queue, deque of an array, etc. interact just with entities.
  • The synchronization: This synchronization in java interacts just with the entities utilized in the concept of multithreading.

Thus, the use of wrapper class in java is mandatory in many fields.

Conversion of primitive to the wrapper class

The procedure of transforming the primitive type of data into its respective wrapper class in java is termed as “Autoboxing.”

For instance, the conversion of char to Character, etc.

Let’s have a grasp on the below illustration that how primitive DT (data type) is transformed into its wrapper class.

public class demo_Wrapper1{
public static void main (String args[]){
// Let’s transform double into Double
Double wc_x= 28.00;
// Let’s transfer double into Double externally
Double wc_d= Double.valueOf(wc_x);
// Let’s carry out autoboxing
 Double wc_d2= wc_x;
System.out.println(wc_x + “ ” + wc_d + “ ” + wc_d2);

Output of the illustration will be:

// 28.00 28.00 28.00

Conversion of the wrapper into primitive types of data

The procedure of transforming the wrapper class in java into its respective primitive type of data is termed as “Unboxing”.

For instance, the conversion of Long to long, Character to char, etc.

Let’s have a grasp on the below illustration that how wrapper class is transformed into its primitive DT (data type).

public class demo_primtive2{
public static void main (String args[]){
// Let’s construct entities of the wrapper class
Double d_x = Double.valueOf (28.78);
Integer i_y = Integer.valueOf (74);
// Let’s transfer into primitive DT
// This Double is transformed into its double primitive type of data
double d_x2 = d_x.intValue();
// This Integer is transformed into its int primitive type of data 
int i_y2 = i_y.intValue();
System.out.println("Output of d_x2 is: " + d_x2);
System.out.println("Output of i_y2 is: " + i_y2);

Output of the illustration will be:

// Output of d_x2 is: 28.78
// Output of i_y2 is: 74

Why only the wrapper class in Java?

A question like “why wrapper class?” can arise in your mind. So, below are the features or reasons for the need for the wrapper class:

  1. The groups of Wrapper transform numeric sequences into numeric values.
  2. An ideal way to store primitive information inside of an entity.
  3. The mechanism valueOf() exists in all wrapper categories excluding Character. 
  4. Every wrapper class has a methodology named typeValue(). As its primitive form, this procedure retains the rightness of the item.


  • When we can’t utilize primitive kind items, the wrapper class just need entities. The compiler transforms the primitive type of the data immediately to the respective Wrapper classes as desired.
  • They sum up more functionality to their respective primitive type of data.
Do you know?
1. Java Queue
2. String to Int Java
3. Super Keyword in Java
4. Fibonacci series in java
5. HashSet in Java with Examples
6. Math.random Java
7. Static Keyword in Java
8. Final Keyword in Java
9. Palindrome in Java
10. Wrapper class in Java

Pin It on Pinterest