What are wrapper classes?

Wrapper classes are object representations of Java's primitive data types. They allow primitive values to be used as objects, enabling them to work with collections, generics, and APIs that require objects instead of primitive types.

Key Points: • Every primitive data type has a corresponding wrapper class. • Wrapper classes are part of the java.lang package. • They enable primitives to be used in Collections Framework classes such as ArrayList and HashMap. • Wrapper classes support utility methods for conversion, parsing, and comparison. • Java provides Autoboxing and Unboxing to automatically convert between primitives and wrapper objects.

Example: An ArrayList cannot store primitive int values directly, so Integer wrapper objects are used instead.

Code Example:

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

public class Demo {

    public static void main(String[] args) {

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

numbers.add(100); // Autoboxing

int value = numbers.get(0); // Unboxing

        System.out.println(value);
    }
}

Primitive to Wrapper Mapping:

• byte → Byte • short → Short • int → Integer • long → Long • float → Float • double → Double • char → Character • boolean → Boolean

Interview Tip: A concise interview answer is:

"Wrapper classes are object versions of primitive data types. They allow primitives to be used where objects are required, such as in collections and generics. Java provides wrapper classes like Integer, Double, Character, and Boolean, along with automatic autoboxing and unboxing support."