Java Wrapper Classes: Primitive Object Wrappers, Autoboxing & Caching

Java's 8 primitive types provide raw computing performance, but they cannot participate in object-oriented structures like the Collections Framework (you cannot create ArrayList<int>). Wrapper classes wrap primitive values inside full-fledged Heap objects.

The 8 Primitive Types and Their Wrapper Counterparts

Primitive Type Wrapper Class (java.lang) Parsing Method Boxing Example
byteByteByte.parseByte("10")Byte b = 10;
shortShortShort.parseShort("20")Short s = 20;
intIntegerInteger.parseInt("100")Integer i = 100;
longLongLong.parseLong("5000")Long l = 5000L;
floatFloatFloat.parseFloat("3.14")Float f = 3.14f;
doubleDoubleDouble.parseDouble("9.99")Double d = 9.99;
charCharacterN/ACharacter c = 'A';
booleanBooleanBoolean.parseBoolean("true")Boolean flag = true;

Autoboxing and Unboxing in Action

import java.util.ArrayList;

public class AutoboxingDemo {
    public static void main(String[] args) {
        // Autoboxing: primitive int 42 automatically converted to Integer object
        Integer numObj = 42;

        // Unboxing: Integer object automatically converted back to primitive int
        int primitiveNum = numObj;

        // Collections require Objects:
        ArrayList<Integer> list = new ArrayList<>();
        list.add(100); // Autoboxed from int to Integer
        int firstItem = list.get(0); // Unboxed from Integer to int
    }
}

The Famous IntegerCache Trap (Interview Favorite)

Java caches Integer objects within the range of -128 to 127. Notice what happens when you compare autoboxed objects with ==:

Integer a = 100;
Integer b = 100;
System.out.println(a == b); // TRUE! (Both point to cached instance)

Integer x = 200;
Integer y = 200;
System.out.println(x == y); // FALSE! (200 is outside cache; distinct Heap objects allocated!)
System.out.println(x.equals(y)); // TRUE! (Always use .equals() for objects!)

University Exam Corner (BCA / B.Sc CS)

Exam Question: "What is the difference between Integer.parseInt("100") and Integer.valueOf("100")?"

Answer: Integer.parseInt() returns a raw primitive int. Integer.valueOf() returns an Integer object and utilizes the IntegerCache pool to save memory when possible.

Next Step

Now let us explore the mathematical utilities of the Math class and pseudo-random numbers. Proceed to: Java Math Class & Random →

Frequently Asked Questions (FAQ)

Q1. What is the purpose of Wrapper Classes in Java?
Wrapper classes encapsulate primitive data types into full-fledged Java Objects. They are mandatory when working with the Java Collections Framework (which only stores Objects, e.g. ArrayList), multithreading, and serialization.
Q2. What is Autoboxing and Unboxing?
Autoboxing is the automatic conversion performed by the compiler from a primitive type into its corresponding wrapper object (e.g. int to Integer). Unboxing is the reverse automatic conversion from a wrapper object back to a primitive value (e.g. Integer to int).
Q3. What is the IntegerCache in Java?
To optimize memory, the JVM maintains an internal cache of Integer objects for values ranging from -128 to +127. When autoboxing integers in this range, the JVM reuses cached instances, meaning == evaluates to true!

Explore Related Java Tutorials

Accelerate your Java mastery by studying these tightly connected core tutorials:

Java Data Types →

The 8 primitive counterparts.

Collections Framework →

Storing wrapper objects in lists.

Java Generics →

Why generics require wrapper objects.

Java Math Class →

Mathematical calculations.

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit

You may also like this!