The Object Array Backing Store
What you'll learn: You'll discover how ArrayList stores its elements internally using a plain Object array, not a primitive array.
The Hidden Array Inside ArrayList
When you create an ArrayList<String>, it looks like a magical, resizable collection. But underneath, it's surprisingly simple: ArrayList uses an ordinary Object[] array to hold your elements.
Why Object[] and not the type you specify? Because of type erasure. At runtime, generics disappear, so ArrayList<String>, ArrayList<Integer>, and ArrayList<Car> all share the same compiled bytecode. They all internally use the same Object[] backing store.
Why Not a Primitive Array?
You might wonder: "Why not use String[] for ArrayList<String>?" Two reasons:
- Type erasure means the generic type parameter vanishes at runtime—there's no way to create an array of the exact generic type
- Code reusability: One ArrayList implementation works for all reference types because
Objectis the universal superclass
Think of it like a warehouse with generic storage bins labeled "Object." Whether you're storing books (Strings), packages (Integers), or furniture (custom objects), the bins themselves are identical. The labels (<String>, <Integer>) exist only at compile-time for type checking.
The Array Is Private
This Object[] is a private field inside ArrayList. When you call add(), ArrayList stores your element in this array. When you call get(), it retrieves from this array and casts back to your type (invisibly). If the array fills up, ArrayList creates a larger Object[] and copies everything over.
Key Takeaway: ArrayList internally uses Object[] as its backing store because type erasure removes generic type information at runtime, making a single, reusable array implementation possible for all reference types.