Question #128134

What are wrapper classes and why are they useful for ArrayLists? In your answer, include examples of autoboxing and unboxing.

Expert's answer

The wrapper class is used to convert primitive types to objects. Since ArrayList is a generic class, it can only interact with objects, which is why wrapper classes for primitive types are used.

ArrayList<Integer> list = new ArrayList<>();
list.add(1);//autoboxing
int first = list.get(0);//unboxing
LATEST TUTORIALS
APPROVED BY CLIENTS