> ## Content Index
> Fetch the complete content index at: https://www.ggorantala.dev/llms.txt
> Use this file to discover other available public pages before exploring further.

# What Are Wrapper Classes in Java?
- URL: https://www.ggorantala.dev/what-are-wrapper-classes-in-java/
- Published: 2023-09-21T15:50:14.000Z
- Updated: 2023-10-03T06:33:51.000Z
- Author: Gopi Gorantala
- Tags: Java

> 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

![](https://storage.ghost.io/c/e3/0b/e30b8112-16a4-4a70-8441-b0c5d5f2b4d7/content/images/2023/09/Screenshot-2023-09-11-at-8.04.23-PM.png)

`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`.

```java
// 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:

```java
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`.

```java
// 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

```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](https://www.ggorantala.dev/java-primitive-and-non-primitive-data-types/).
2. [Autoboxing and Auto-unboxing in Java](https://www.ggorantala.dev/auto-boxing-and-auto-unboxing-in-java/)
3. [How to convert String to int in Java?](https://www.ggorantala.dev/how-to-convert-string-to-int-in-java/)
4. [Why Wrapper Classes Are Used Rather Than Primitives?](https://www.ggorantala.dev/why-wrapper-classes-are-used-instead-primitives-in-prod-applications/)