Code360 powered by Coding Ninjas X Code360 powered by Coding Ninjas X
Table of contents
Wrapper Classes
The eight wrapper classes
Uses of Wrapper Class
Wrapper class methods
Demonstration of Wrapper Class methods
Key Takeaways
Last Updated: Mar 27, 2024

Wrapper class in Java


Data types in Java are divided into two types, i.e., Primitive and Non-primitive data types.

Primitive data types are the data types that are already defined(predefined), whereas non-primitive data types are the data types that are defined by the programmers themselves, not by Java.

In Java, to provide some additional features, a concept of the Wrapper class over primitive data types got introduced. Let us learn more about the Wrapper class in this blog.

Also see, procedure call in compiler designDuck Number in Java

Wrapper Classes

The Wrapper class in Java is used to wrap primitive data types to objects. Basically, it provides a mechanism to convert primitive values into objects and objects into primitive values. The wrapper class can be understood with the following two concepts.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job


When primitive values are converted into respective wrapper class objects, this procedure is called Autoboxing

For Example: char to Character, int to Integer, long to Long, double to Double, etc.

Let’s understand autoboxing with a sample of code:


public class AutoboxingEg {
  public static void main(String[] args) {
    // Autoboxing int to Integer. 
    int tmp = 15;
    Integer ntmp = tmp;
    System.out.println("Integer: " + ntmp);

    // Autoboxing char to Character.   
    char ch = 'p';
    Character nch = ch;
    System.out.println("Character: " + nch);


Integer: 15
Character: p


Also see, Hashcode Method in Java  and  Swap Function in Java


Unboxing is the reverse process of Autoboxing, i.e., when an object of the wrapper class is converted to its respective primitive value, the procedure is known as Unboxing.

For Example: Boolean to boolean, Float to float, Integer to integer, etc.

The below program depicts the use of unboxing.


import Java.util.ArrayList;
public class UnboxingEg {
 public static void main(String[] args) {
   //Unboxing Integer into int.
   Integer tmp = 205;
   int ntmp = d;
   System.out.println("int: " + ntmp); 

   //Unboxing Double into double.
   Double c = 27.8;
   double nc = c;
   System.out.println("double: " + nc); 


int: 205
double: 27.8 

The eight wrapper classes

Primitive Data type Wrapper class
boolean Boolean
byte Byte
char Char
double Double
float Float
int Integer
long Long
short Short

Uses of Wrapper Class

  • When we need to modify the arguments passed in to a method, we can do it by passing them in the form of wrapper class objects.
  • Java.util package‘s classes only deal with objects, so Wrapper class is required.
  • In multithreading also, an object is needed to support synchronization, so again we need to convert primitive values into objects.
  • In primitive data structures, we can’t store null values, whereas, in the Wrapper class objects, we can store null values.
  • In collection classes like LinkedList, ArrayList, etc., which store only objects and not primitive values, a wrapper class is used.
  • Requirements like when we want to convert one primitive data type to another primitive data type, we can do it with the help of wrapper class. For example, if we want to create an integer value from some string, we can perform it with the help of wrapper class utility “.valueOf()”.

Wrapper class methods

Method Description
Integer valueOf(int i) Returns the Integer object of the specified int primitive value.
String toString() Returns a string representation of the specified value.

byte ByteValue()

short ShortValue()

int intValue()

long longValue()

float floatValue()

double doubleValue()

Returns the value of this Number object into its corresponding primitive type

int compareTo(Byte byte)

int compareTo(Double double)

int compareTo(Float float)

int compareTo(Long long)

int compareTo(Integer int)

int compareTo(Short short)

Compares this Number object with the specified argument

Demonstration of Wrapper Class methods


// Program to show Wrapper class methods.

// Main Class
public class wrapperClass {

      // Main driver method
      public static void main(String[] args)
            // Conversion of int to Integer with the help of valueOf() utility.
            Integer val = Integer.valueOf(30);

            // Conversion of float to Float with the help of valueOf() utility.
            Float fVal = Float.valueOf(6.78f);

            // Conversion of the binary number to an integer value.
            Integer bin2int = Integer.valueOf("1000", 2);



Try it on java online compiler.

Key Takeaways

The above blog describes the use and features of the Wrapper class, which helps you to learn about new utilities in Java and gain results that can’t be achieved with primitive data types.

If you want to learn more about such concepts, head over to our library section for many such interesting blogs. Keep learning.

Happy Coding!


Live masterclass