Java Autoboxing and Unboxing: Simplifying Primitive and Wrapper Conversions

Understand the concepts of autoboxing and unboxing in Java, which facilitate automatic conversion between primitive values and their corresponding wrapper objects. Learn how an int can be seamlessly assigned to an Integer object, eliminating the need for explicit casting and method calls. Discover how these features enhance code readability and efficiency.



Java - Autoboxing and Unboxing

Java Autoboxing

Autoboxing is a feature in Java that allows the compiler to automatically convert a primitive value to its corresponding wrapper object. This means, for example, that an int value can be assigned to an Integer object without the need for explicit casting or method calls. Autoboxing is also known as boxing.

Autoboxing Example

        // Autoboxing Example
        Integer obj = 20;

        // Explicit Casting
        Integer obj2 = Integer.valueOf(20);
        

In both cases, the Integer object is initialized with an int value. The first case demonstrates autoboxing, while the second case uses explicit casting.

When Does the Compiler Use Autoboxing?

  • When a primitive value is passed as an argument to a method expecting a wrapper class object.
  • When a primitive value is assigned to a variable of type wrapper class.

Example of Autoboxing in Java

In this example, we create a list of Integer objects. Java automatically converts the primitive int values to Integer objects when adding them to the list.

Autoboxing Code Example

        package com.tutorialsarena;

        import java.util.ArrayList;
        import java.util.List;

        public class Tester {
            public static void main(String[] args) {
                List list = new ArrayList<>();

                for(int i = 0; i < 10; i++){
                    // Autoboxing: int to Integer
                    list.add(i);
                }

                System.out.println(list);

                char c = 'b';
                // Autoboxing: char to Character
                Character ch = c;
                System.out.println(ch);
            }
        }
        
Output

        [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
        b
        

Java Unboxing

Unboxing is the reverse process of autoboxing. The Java compiler automatically converts a wrapper object to its corresponding primitive value. For example, when an Integer object is passed to a method expecting an int, the compiler automatically unboxes the Integer to an int.

Unboxing Example

        Integer obj = Integer.valueOf(20);

        // Unboxing
        int i = obj;

        // Explicit Value Deduction
        i = obj.intValue();
        

In both cases, the primitive values are initialized with an int. The first case shows unboxing, while the second case uses explicit method calls.

When Does the Compiler Use Unboxing?

  • When a wrapper class object is passed as an argument to a method expecting a primitive value.
  • When a wrapper class object is assigned to a primitive variable.

Example of Unboxing in Java

In this example, we pass an Integer object to a method that expects an int primitive. Java automatically converts the Integer object to an int value.

Unboxing Code Example

        package com.tutorialsarena;

        public class Tester {
            public static void main(String[] args) {
                Integer integer = Integer.valueOf(-20);
                // Unboxing: Integer to int
                int i = abs(integer);        
                System.out.println(i);

                // Unboxing: Integer to int
                int j = integer;        
                System.out.println(j);
            }
            
            private static int abs(int i){
                return (i < 0)? -i: i;
            }
        }
        
Output

        20
        -20
        

Mapping of Primitive and Wrapper Objects

Primitive Wrapper Method to Get Value
byte Byte byteValue()
short Short shortValue()
int Integer intValue()
long Long longValue()
float Float floatValue()
double Double doubleValue()
char Character charValue()
boolean Boolean booleanValue()