Java Fundamentals

Wrapper Classes, Autoboxing & Unboxing

Prepared By Krishna Srikanth M

1. Wrapper Classes in Java

A wrapper class in Java provides the mechanism to convert a primitive value into an object and an object into a primitive value.

Since J2SE 5.0, Java supports autoboxing and unboxing, which automatically convert primitives into their corresponding wrapper objects and wrapper objects back into primitives.

Primitive
Autoboxing
Wrapper Object
Wrapper Object
Unboxing
Primitive
Simple definition: Autoboxing means primitive → wrapper object. Unboxing means wrapper object → primitive.
Wrapper classes overview from PDF

2. Uses of Wrapper Classes in Java

Java is an object-oriented programming language, so wrapper classes are useful whenever Java APIs require objects rather than primitive values.

  • Changing a value in a method: Java supports call by value. The source explains the use of objects when object-based behavior is required.
  • Serialization: Primitive values can be represented as objects when working with object streams.
  • Synchronization: Java synchronization works with objects in multithreading.
  • java.util package: Utility classes commonly deal with objects.
  • Collection Framework: Collections such as ArrayList, LinkedList, Vector, HashSet, TreeSet, PriorityQueue and ArrayDeque work with objects.
Important: Java Collections use wrapper/reference types rather than primitive types such as int and double.

3. Wrapper Class Types

The eight primitive types and their corresponding wrapper classes are:

Primitive TypeWrapper Class
booleanBoolean
charCharacter
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble

Since Java 5, explicit valueOf() calls are generally unnecessary for ordinary primitive-to-wrapper conversion because autoboxing can perform the conversion automatically.

Wrapper class mapping and example from PDF

4. Java Wrapper Classes Example

The PDF demonstrates conversion of all primitive types into their corresponding wrapper objects and then back into primitive values.

public class WrapperExample3 {
    public static void main(String args[]) {

        byte b = 10;
        short s = 20;
        int i = 30;
        long l = 40;
        float f = 50.0F;
        double d = 60.0D;
        char c = 'a';
        boolean b2 = true;

        // Autoboxing: Converting primitives into objects
        Byte byteobj = b;
        Short shortobj = s;
        Integer intobj = i;
        Long longobj = l;
        Float floatobj = f;
        Double doubleobj = d;
        Character charobj = c;
        Boolean boolobj = b2;

        // Printing objects
        System.out.println("---Printing object values---");
        System.out.println("Byte object: " + byteobj);
        System.out.println("Short object: " + shortobj);
        System.out.println("Integer object: " + intobj);
        System.out.println("Long object: " + longobj);
        System.out.println("Float object: " + floatobj);
        System.out.println("Double object: " + doubleobj);
        System.out.println("Character object: " + charobj);
        System.out.println("Boolean object: " + boolobj);

        // Unboxing: Converting Objects to Primitives
        byte bytevalue = byteobj;
        short shortvalue = shortobj;
        int intvalue = intobj;
        long longvalue = longobj;
        float floatvalue = floatobj;
        double doublevalue = doubleobj;
        char charvalue = charobj;
        boolean boolvalue = boolobj;

        // Printing primitives
        System.out.println("---Printing primitive values---");
        System.out.println("byte value: " + bytevalue);
        System.out.println("short value: " + shortvalue);
        System.out.println("int value: " + intvalue);
        System.out.println("long value: " + longvalue);
        System.out.println("float value: " + floatvalue);
        System.out.println("double value: " + doublevalue);
        System.out.println("char value: " + charvalue);
        System.out.println("boolean value: " + boolvalue);
    }
}

What this example demonstrates

  • Primitive values are assigned to wrapper references using autoboxing.
  • Wrapper references are assigned to primitive variables using unboxing.
  • The same eight primitive-wrapper pairs can be used automatically.
Wrapper class conversion example from PDF

5. Autoboxing

Autoboxing refers to the automatic conversion of a primitive type variable into its corresponding wrapper class object.

The compiler automatically handles this conversion when a primitive value is:

  • Passed as an argument to a method that expects a wrapper class object.
  • Assigned to a variable whose type is a wrapper class.

Example: Assigning an int to Integer

int a = 20;

Integer i = Integer.valueOf(a); // explicit conversion
Integer j = a;                  // autoboxing

System.out.println(a + " " + i + " " + j);

Output

20 20 20
Compiler behavior: When autoboxing is used, the compiler effectively performs the corresponding wrapper conversion automatically.

6. Autoboxing Examples

Example 1 — ArrayList

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

public class Tester {
    public static void main(String[] args) {

        List<Integer> list = new ArrayList<>();

        for(int i = 0; i < 10; i++) {
            // int value is converted to Integer
            // by the compiler during compilation
            list.add(i);
        }

        System.out.println(list);

        char c = 'a';

        // char is converted to Character
        Character ch = c;

        System.out.println(ch);
    }
}

Output

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

Example 2 — ArrayList with Integer values

import java.io.*;
import java.util.*;

class GFG {
    public static void main(String[] args) {

        ArrayList<Integer> al = new ArrayList<Integer>();

        // Adding int primitive values
        // using add() method — autoboxing
        al.add(1);
        al.add(2);
        al.add(24);

        System.out.println("ArrayList: " + al);
    }
}

Output

ArrayList: [1, 2, 24]
Autoboxing examples and output from PDF

7. Unboxing

Unboxing is the reverse of autoboxing. It refers to the automatic conversion of a wrapper object into its corresponding primitive variable.

The compiler automatically handles this conversion when a wrapper object is:

  • Passed as an argument to a method that expects a primitive data type.
  • Assigned to a variable whose type is a primitive data type.
Wrapper Object
Unboxing
Primitive Variable
Unboxing explanation and example from PDF

8. Unboxing Example

public class Tester {
    public static void main(String[] args) {

        Integer integer = new Integer(-10);

        // Integer object is converted to int
        // by compiler during compilation
        int i = abs(integer);

        System.out.println(i);

        // Integer object assigned to int variable
        int j = integer;

        System.out.println(j);
    }

    private static int abs(int i) {
        return (i < 0) ? -i : i;
    }
}

Output

10
-10
Why? When integer is passed to abs(int), it is automatically unboxed from Integer to int. The assignment int j = integer; also performs automatic unboxing.
Unboxing code and output from PDF

9. Quick Revision

ConceptMeaningExample
Wrapper ClassObject representation of a primitive type.int → Integer
AutoboxingAutomatic primitive-to-wrapper conversion.Integer x = 10;
UnboxingAutomatic wrapper-to-primitive conversion.int x = integer;
CollectionsCollection classes work with objects/reference types.List<Integer>

Primitive → Wrapper Mapping

PrimitiveWrapper
booleanBoolean
charCharacter
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble

Remember

Primitive
   |
   | Autoboxing
   v
Wrapper Object
   |
   | Unboxing
   v
Primitive