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 Manager with over 14 years of extensive expertise in Java-based applications. He resides in Europe and specializes in designing and scaling high-performance applications.

Comments


Related Posts

Members Public

Spring Boot Hello World Tutorial with Lombok and H2 Database – Quick Start for Beginners

Learn how to create a Hello World Spring Boot application using Lombok for cleaner code and the H2 in-memory database for rapid development. This step-by-step guide includes annotations, project setup, REST API, H2 console access, and more to kickstart your Spring Boot journey.

Spring Boot Hello World Tutorial with Lombok and H2 Database – Quick Start for Beginners
Members Public

NoSuchBeanDefinitionException: The Most Common Spring Bean Error

Learn how to fix NoSuchBeanDefinitionException in Spring Boot with clear examples and best practices for dependency injection, package scanning, and bean registration.

NoSuchBeanDefinitionException for a missing bean in the ApplicationContext.
Members Public

BeanCurrentlyInCreationException: Circular Dependencies in Spring

Spring Boot’s DI can backfire with the dreaded BeanCurrentlyInCreationException, signaling an unresolvable circular dependency. Learn why cycles occur, how they breach SOLID principles, and three fixes—refactor to break loops, use @Lazy injection, and static @Bean factory methods—to restore startup.

Circular dependencies in Spring causing a BeanCurrentlyInCreationException