Skip to content

What Are Wrapper Classes In Java?

3 min read

Table of Contents

This is the most asked interview question! Why do we use wrapper classes to store data for a model class instead of primitives?

What are wrapper classes?

Primitive wrappers, also known as wrapper classes, are a set of classes in Java that provide an object representation for the primitive data types.

Wrapper classes are all final, so once created, you cannot alter the underlying value. They are immutable. Autoboxing and unboxing use value copy. There are no shared values or references. Every boxing operation is a copy.

They allow you to treat primitive types as objects. This is necessary in situations where you need to use objects rather than primitives, such as in collections (like ArrayList or LinkedList) or when you want to take advantage of object-oriented features like inheritance and polymorphism.

Wrapper classes are part of the java.lang package and are automatically imported into any Java program, so you don't need to import them explicitly.

Sketch / Illustration

Boolean and Character are subtypes of the Object class.

But the other primitive wrappers Byte, Short, Integer, Long, Float, and Double are all subtypes of Number class.

Primitives & their wrappers

There is a wrapper class available for each of the primitive types.

  1. Integer types
    1. byte -> The wrapper is java.lang.Byte.
    2. short -> The wrapper is java.lang.Short.
    3. int -> The wrapper is java.lang.Integer.
    4. long -> The wrapper is java.lang.Long.
  2. Floating point types
    1. float -> The wrapper is java.lang.Float.
    2. double -> The wrapper is java.lang.Double.
  3. Character type
    1. char -> The wrapper is java.lang.Character.
  4. Boolean type
    1. boolean -> The wrapper is java.lang.Boolean.

🔥 Why Use Wrapper Classes?

Wrapper classes serve several purposes, including:

  1. Facilitating use in Java generics: Generic types in Java can only work with objects, not primitive types. Wrapper classes enable you to use primitive types in generic classes.
  2. Allowing primitive types to be used as objects: In situations where objects are expected (e.g., collections, generics, or method parameters), wrapper classes enable you to use primitive types.
  3. Providing utility methods: Wrapper classes provide methods for converting, parsing, and manipulating primitive values. For example, Integer class provides methods like parseInt(), toString(), etc.
  4. Supporting null values: Primitive data types cannot represent null values, but wrapper classes can, allowing you to represent the absence of a value.

Auto-Boxing and Auto-Unboxing

Java also provides a feature called auto-boxing and auto-unboxing. This allows the conversion between primitive types and their corresponding wrapper classes to happen automatically when needed.


The process of converting a primitive type to its corresponding wrapper class automatically. For example, int to Integer.

// Autoboxing: converting int to Integer
int primitiveInt = 10;
Integer wrappedInt = primitiveInt; // Autoboxing

Behind the scenes, this is equivalent to explicitly creating an Integer object like this:

Integer wrappedInt = new Integer(primitiveInt); 


The process of converting a wrapper class object to its corresponding primitive type automatically. For example, Integer to int.

From Integer to int.

// Unboxing: converting Integer to int
Integer wrappedInt = 20;
int primitiveInt = wrappedInt; // Unboxing

These features make it more convenient to work with both primitive types and their wrapper classes in Java.


public class WrapperExample {
    public static void main(String[] args) {
        // Creating instances of wrapper classes
        Integer intWrapper = new Integer(10); // Deprecated in Java 9 and removed in Java 11
        Integer intWrapperNew = Integer.valueOf(10); // Preferred way to create Integer objects
        Double doubleWrapper = new Double(3.14);
        Character charWrapper = new Character('A');
        Boolean boolWrapper = new Boolean(true);
        // Autoboxing (converting primitive to wrapper)
        Integer anotherIntWrapper = 20; // Autoboxing - int to Integer
        Double anotherDoubleWrapper = 5.0; // Autoboxing - double to Double
        Boolean anotherBoolWrapper = true; // Autoboxing - boolean to Boolean
        // Unboxing (converting wrapper to primitive)
        int primitiveInt = intWrapperNew.intValue(); // Unboxing - Integer to int
        double primitiveDouble = doubleWrapper.doubleValue(); // Unboxing - Double to double
        char primitiveChar = charWrapper.charValue(); // Unboxing - Character to char
        // Using wrapper classes in collections
        ArrayList<Integer> intList = new ArrayList<>();
        intList.add(30); // Autoboxing - int to Integer
        int value = intList.get(0); // Unboxing - Integer to int
        // Wrapper classes can be helpful when working with APIs that require objects, such as collections or generics.

Additional resources

  1. Java primitive and Non-primitive data types.
  2. Autoboxing and Auto-unboxing in Java
  3. How to convert String to int in Java?
  4. Why Wrapper Classes Are Used Rather Than Primitives?

ggorantala Twitter

Gopi has a decade of experience with a deep understanding of Java, Microservices, and React. He worked in India & Europe for startups, the EU government, and tech giants.


Related Posts

Members Public

How To Convert String To Integer in Java?

In Java, you can convert a String to a Integer object using valueOf(...) method provided by Integer wrapper. Before diving into String to Integer conversions, if you want to catch up on the basics of primitives and String to int conversions, please check the additional resources section. String to Integer

Members Public

How To Convert String To int in Java?

In Java, you can convert a String to a int primitive type using various methods provided by the Java standard library. Before diving into String conversion to primitive types. If you want to catch up on the basics of primitives, please check the additional resources section. To convert a Java

Members Public

What Are Java Features?

Java is a widely used, high-level programming language known for its versatility and platform independence. Its robust features suit various applications, from web development to enterprise-level software. Key features The key features collectively contribute to Java's popularity and wide adoption across a diverse range of industries and application domains. Simple