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

# Java Autoboxing and Unboxing
- URL: https://www.ggorantala.dev/auto-boxing-and-auto-unboxing-in-java/
- Published: 2023-09-06T18:58:00.000Z
- Updated: 2023-10-04T11:14:46.000Z
- Author: Gopi Gorantala
- Tags: Java

Java Auto-Boxing and Auto-unboxing are features introduced in Java 5 (JDK 5) to simplify working with primitives and objects.

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.

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.

## **Autoboxing**

The process of converting a primitive type to its corresponding wrapper class automatically. Here are a couple of examples:

### Integer Autoboxing

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

### Double Autoboxing

```java
// Autoboxing: converting double to Double
double primitiveDouble = 3.14;
Double wrappedDouble = primitiveDouble; // Autoboxing

```

Here, the primitive `double` value `3.14` is automatically converted to a `Double` object. Behind the scenes, this is equivalent to explicitly creating an `` `Double` `` object like this:

```java
Double wrappedDouble = new Double(primitiveDouble);
```

## **Auto-unboxing**

The process of converting a wrapper class object to its corresponding primitive type automatically. 

Here are a couple of examples:

### Integer Auto-unboxing

From `Integer` to `int`.

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

In this example, the `Integer` object `wrappedInt` is automatically unboxed to its primitive `int` value. Behind the scenes, this is equivalent to explicitly calling the `intValue()` method like this:

```java
int value = wrappedInt.intValue();

```

### Boolean Auto-unboxing

```java
Boolean flag = true; // Auto-boxing
boolean isFlag = flag; // Auto-unboxing: Boolean to boolean

```

In this case, the `Boolean` object `flag` is automatically unboxed to its primitive `boolean` value.

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

## Auto-boxing and unboxing in collections

Autoboxing and unboxing are particularly useful when working with collections, as collections can only hold objects, not primitives.

```java
// Autoboxing in ArrayList
ArrayList<Integer> intList = new ArrayList<>();
intList.add(30); // Autoboxing: int to Integer

// Unboxing from ArrayList
int value = intList.get(0); // Unboxing: Integer to int

```

## Benefits and Considerations

1. **Convenience**: Autoboxing and auto-unboxing make it more convenient to work with both primitives and objects, reducing the need for explicit conversions.
2. **Readability**: The code becomes more readable and concise as you can use primitives and their corresponding wrapper classes interchangeably.
3. **Compatibility with Libraries**: Many libraries and APIs expect objects, so autoboxing and auto-unboxing help seamlessly integrate with such code.

## Caution

1. **Performance Overhead**: In situations where performance is critical, be cautious, as autoboxing and auto-unboxing can incur a small performance overhead compared to working directly with primitives.
2. **Potential for NullPointerException**: When working with wrapper objects, there's a possibility of encountering a `NullPointerException` if the object is `null` and you try to perform auto-unboxing.

Autoboxing and auto-unboxing are powerful features that enhance the flexibility and readability of Java code. However, it's important to use them judiciously, especially in performance-sensitive scenarios, and be mindful of potential pitfalls.

## Additional Resources

1. [Java primitive and Non-primitive data types](https://www.ggorantala.dev/java-primitive-and-non-primitive-data-types/).
2. [How to convert String to int in Java?](https://www.ggorantala.dev/how-to-convert-string-to-int-in-java/)
3. [How To Convert String To Integer in Java?](https://www.ggorantala.dev/how-to-convert-string-to-integer-in-java/)
4. [What are wrapper classes in Java?](https://www.ggorantala.dev/what-are-wrapper-classes-in-java/)