Table of contents
1.
Introduction
2.
Use Cases
2.1.
Program
2.2.
Output
2.3.
Program
2.4.
Output
3.
Autoboxing and Unboxing in Comparisons
3.1.
Program
3.2.
Output
4.
Autoboxing and Unboxing in Method Argument Passing
4.1.
Program
4.2.
Output
5.
Operators with Special Requirements
5.1.
Program
5.2.
Output
6.
Frequently Asked Questions
6.1.
Why do we need Autoboxing?
6.2.
What is boxing and unboxing in Java?
6.3.
What is Java autoboxing?
7.
Conclusion
Last Updated: Sep 5, 2024
Easy

Autoboxing and Unboxing

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

This blog discusses a new feature of Java SE 5, namely, Autoboxing and Unboxing. Let us first see the definition of these two features:

Autoboxing: Autoboxing or boxing is the conversion of primitive data types into objects of the corresponding Wrapper class.

Example: Conversion of int to an object of the Integer class.

Unboxing: Unboxing is the opposite of boxing. It involves the conversion of objects of Wrapper classes to their corresponding primitive data types.

Example: Converting an object of Integer class to int.

Autoboxing and Unboxing

Autoboxing / Unboxing has several advantages.

  1. Developers can build cleaner, easier-to-read code by using autoboxing and unpacking.
  2. We may utilise primitive types and Wrapper class objects thanks to this method interchangeably, eliminating the requirement for explicit typecasting.
     

Also see, Duck Number in Java  and  Swap Function in Java

Use Cases

Let us consider a few examples to better understand these data type conversions in Java.

Example of Boxing: Conversion of int and char variables to objects of Integer and Character classes.

Program

class AutoboxingExample
{
    public static void main (String[] args)
    {
        // Create an int data type variable.
        int intVar = 10;
  
        // Autoboxing the int variable to create an object of the Integer class with the same value.
        Integer intObject = intVar;
  
        System.out.println("Value of int data type variable: " + intVar);
        System.out.println("Value of Integer Object " + intObject);
        System.out.println();
  
        // Create a char data type variable.
        char charVar = 'a';
  
        // Autoboxing the char variable to create an object of the Character class with the same value.
        Character charObject = new Character('a');
  
        System.out.println("Value of char data type variable: " + charVar);
        System.out.println("Value of Character Object " + charObject);
  
    }
}
You can also try this code with Online Java Compiler
Run Code

Output

Value of int data type variable: 10

Value of Integer Object 10

Value of char data type variable: a
Value of Character Object a

Example of Unboxing: Conversion of an object of Integer and Character class to int and char, respectively.

Program

class UnboxingExample
{
    public static void main (String[] args)
    {
        // Create an object of the Integer class.
        Integer intObject = new Integer(5);
  
        // Unboxing the object by equating it to an int data type variable.
        int intVar = intObject;
  
        System.out.println("Value of Integer Object " + intObject);
        System.out.println("Value of int data type variable: " + intVar);
        System.out.println();
  
        // Create an object of the Character class.
        Character charObject = new Character('a');
  
        // Unboxing the object by equating it to an char data type variable.
        char charVar = charObject;
  
        System.out.println("Value of Character Object " + charObject);
        System.out.println("Value of char data type variable: " + charVar);
  
    }
}
You can also try this code with Online Java Compiler
Run Code

Output

Value of Integer Object 5
Value of int data type variable: 5


Value of Character Object a
Value of char data type variable: a

Autoboxing and Unboxing in Comparisons

For the compiler to compare two entities, it is necessary that the entities are of the same data type. If one entity is an object of any wrapper class, the second must also be an object of the same class. The same holds for primitive data types as well.

When we compare an object of, let's say, Integer class with an int data type variable, the compiler internally performs autoboxing or unboxing operations to make the two entities of the same data type.

Let us see an example to clarify this point.

Program

class UnboxingExample2 {


  public static void main(String args[]) {
    // Create an object of the Integer class.
    Integer intObject = new Integer(50);


    // While performing this comparison, intObject is internally converted to int data type, i.e., Unboxing.
    if (intObject < 100) {
      System.out.println(intObject);
    }


  }
}
You can also try this code with Online Java Compiler
Run Code

Output

50

Autoboxing and Unboxing in Method Argument Passing

The compiler performs autoboxing and unboxing operations, if necessary, during method calls. Let's say a method expects an argument of int data type as its first argument. However, in the method call, an object of the Integer class is passed as the first argument. In such a scenario, the compiler performs an unboxing operation to convert the object of the Integer class to int data type.

Let us see an example to clarify this point.

Program

class MethodArgumentExample {


  public static void printSum(int a, int b) {
    // Compute the sum and print it.
    int sum = a + b;
    System.out.println("Sum of a and b: " + sum);
  }


  public static void main(String[] args) {
    // Create two objects of the Integer class.
    Integer intObj1 = new Integer(5);
    Integer intObj2 = new Integer(10);


    // Print the sum.
    // Here autoboxing operation is performed to convert intObj1 and intObj2 to int data types.
    printSum(intObj1, intObj2);
  }
}
You can also try this code with Online Java Compiler
Run Code

Output

Sum of a and b: 15

Operators with Special Requirements

Few operators such as remainder (%) and unary plus (+=) can not be applied to Integer class objects. Whenever we apply these operators involving Integer class objects, the compiler automatically performs unboxing operations to convert them to int variables.

Let us see an example to understand this better.

Program

// Java program to illustrate find sum
// of odd numbers using autoboxing and unboxing


class SpecialOperatorExample {


  public static void main(String[] args) {
    // Create two objects of the Integer class.
    int sum = 0;
    Integer intObj2 = new Integer(10);


    // Unboxing operation is performed to convert intObj2 to int data type and add it to the sum.
    sum += intObj2;


    // No unboxing or autoboxing.
    sum += 10;


    System.out.println("The sum is: " + sum);
  }
}
You can also try this code with Online Java Compiler
Run Code

Output

The sum is: 20


You can practice by yourself with the help of online java compiler.

Frequently Asked Questions

Why do we need Autoboxing?

Autoboxing simplifies code by automatically converting primitive types to their corresponding wrapper class objects, allowing easier integration between primitives and collections or APIs that require objects.

What is boxing and unboxing in Java?

Boxing is the process of converting a primitive type to its corresponding wrapper class object, while unboxing is the reverse, converting a wrapper object back to its primitive type.

What is Java autoboxing?

Java autoboxing is the automatic conversion of primitive types (e.g., int, char) into their corresponding wrapper class objects (e.g., Integer, Character), reducing manual type conversions in the code.

Conclusion

In this blog, we discussed Autoboxing and Unboxing features of the Java programming language. Autoboxing and Unboxing operations are applied automatically by the compiler internally. However, it is necessary to understand such conversions to simplify our codes. We saw different examples where autoboxing and unboxing operations are performed.

Check out this problem - Subarray Sum Divisible By K

So head over to our practice platform Code360 to practice top problems, attempt mock tests, read interview experiences, and much more.

Live masterclass