Skip to content

What Are Wrapper Classes in Java?

Gopi Gorantala
Gopi Gorantala
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.

Auto-boxing

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); 

Auto-unboxing

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.

Example

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?
Java

Gopi Gorantala Twitter

Gopi is an engineering leader with 12+ of experience in full-stack development—a specialist in Java technology stack. He worked for multiple startups, the European govt, and FAANG in India and Europe.

Comments


Related Posts

Members Public

Differences Between JDK, JRE, and JVM?

Short answer JDK, JRE, and JVM are essential components of the Java platform, each serving a distinct purpose. Here are the key differences between them: 1. JDK (Java Development Kit): The JDK is used by developers to write, compile, and debug Java code. 2. JRE (Java Runtime Environment): End-users use

Members Public

Difference Between String and char[] in Java

Short answer Strings String is an object with many helpful methods. String class in Java's standard library is designed to handle text as a sequence of characters. A string of characters (string object) is non-modifiable or immutable in Java. Once you've created it, you cannot modify

Members Public

What is an Object class in Java?

Short answer Object class is the super class of every class you can create. In Java, a class extends another class using the keyword extends. If you don't have any other class to extend, that's fine. The compiler will make your class extend the Object class.