C

Core Java tutorial for beginners

Clean • Professional

Java - Wrapper Classes and Autoboxing/Unboxing (With Examples)

4 minute

Wrapper Classes & Autoboxing / Unboxing in Java

In Core Java, Wrapper Classes are part of the java.lang package. They are used to convert primitive data types (like int, char, double) into objects, and vice versa.

This conversion allows primitives to work in Java’s Collections Framework and other object-oriented contexts.


What Are Wrapper Classes?

A Wrapper Class “wraps” (or encapsulates) a primitive data type into an object.

Each primitive type in Java has a matching wrapper class:

learn code with durgesh images

Primitive TypeWrapper Class
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
charCharacter
booleanBoolean

Why Use Wrapper Classes?

  • To use primitive data in collections (like ArrayList, HashMap) — since collections work only with objects.
  • To use utility methods like Integer.parseInt(), Double.valueOf(), etc.
  • For data conversion between primitives, Strings, and objects.
  • Useful in serialization, reflection, and generics.

Example – Primitive to Wrapper (Boxing)

public class Example {
    public static void main(String[] args) {
        int num = 10;                     // primitive
        Integer obj = Integer.valueOf(num);  // manual boxing
        System.out.println(obj);
    }
}

Output:

10

Here, the primitive int is converted into an Integer object manually using Integer.valueOf().


Autoboxing (Automatic Conversion)

Autoboxing is the automatic conversion of a primitive type to its wrapper class object by the Java compiler.

public class AutoBoxingExample {
    public static void main(String[] args) {
        int a = 5;
        Integer obj = a; // autoboxing
        System.out.println("Integer object: " + obj);
    }
}

Output:

Integer object: 5

Note : The compiler automatically wraps int a into an Integer object — no need for Integer.valueOf() manually.


Unboxing (Object → Primitive)

Unboxing is the reverse process — converting a wrapper object back into a primitive value.

public class UnboxingExample {
    public static void main(String[] args) {
        Integer obj = 20;   // autoboxing
        int num = obj;      // unboxing
        System.out.println("Primitive int: " + num);
    }
}

Output:

Primitive int: 20

The wrapper Integer obj is automatically converted back to int.


Autoboxing & Unboxing Together

import java.util.*;

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

        // Autoboxing: int → Integer
        list.add(10);
        list.add(20);
        list.add(30);

        // Unboxing: Integer → int
        for (int num : list) {
            System.out.println(num);
        }
    }
}

Output:

10
20
30

Note : Java automatically performs both conversions — keeping your code clean and readable.


Useful Wrapper Class Methods

MethodDescriptionExample
Integer.parseInt("100")Converts String → intint n = Integer.parseInt("100");
Integer.valueOf(100)Returns Integer objectInteger obj = Integer.valueOf(100);
Double.parseDouble("10.5")Converts String → doubledouble d = Double.parseDouble("10.5");
Character.isDigit('5')Checks if a character is a digittrue
Boolean.parseBoolean("true")Converts String → booleantrue

Differences Between Primitive & Wrapper

FeaturePrimitiveWrapper Class
Stored InStack memoryHeap memory
Default Value0, falsenull
Can Be NullNoYes
Used in CollectionsNoYes
TypeValue typeObject type
PerformanceFasterSlightly slower (object overhead)

Common Mistakes / Pitfalls

ProblemDescription
NullPointerExceptionUnboxing a null wrapper object causes an error.
Performance OverheadFrequent boxing/unboxing can reduce speed.
Equality Confusion== compares object references, not values — use .equals() for value comparison.

Points to Remember

  • Wrapper classes make primitive data usable as objects.
  • Autoboxing/unboxing simplifies conversion automatically.
  • Collections and Generics require objects, not primitives.
  • Wrapper objects are immutable — their values cannot be changed once created.
  • Use .equals() to compare wrapper values, not ==.

Article 0 of 0