Lesson · Arrays
Arrays under the hood: header, bounds check, covariance
11 min readArrays Internals
What an array object actually contains, why indices are safe, and the one type hole in the system: array covariance and the ArrayStoreException it buys.
What is in an array object
Every array is a heap object with a small header: the class pointer (whose element type it is) and a final length field. The data follows immediately: a contiguous block of slots, four bytes each for int, eight for long or reference, one for boolean. One array is one allocation, so its elements sit side by side in memory, which is why scanning an int[] is the fastest iteration Java offers.
The contiguity explains the JIT's tricks: it unrolls small loops, vectorises scalable loops, and lifts the bounds check when the index is provably between 0 and length. array.length is a final field, so reading it inside a loop is a constant the optimiser can use, one reason the field-not-method design turned out well.
Array object · header, slots, checks
new int[4] allocates one heap object. It begins with a header, and the four slots below it are the data. This single picture is the whole data structure.
Bounds checking, the JIT, and hot loops
Each a[i] reads the header's length and compares i against 0 and length, and throws ArrayIndexOutOfBoundsException if outside. This is the memory-safety guarantee that C lacks. The check seems like pure overhead until you notice the JIT's analysis: when i ranges 0 to a.length - 1 with a constant step, the compiler proves every future check redundant and removes them for the whole loop.
The practical consequence: write loops the optimiser can prove. The classic for (int i = 0; i < a.length; i++) is exactly the shape it recognises. Obscuring the bound (if (i <= a.length - 1)) costs nothing either, but an index computed from unrelated variables keeps the checks in.
Array covariance: the type hole
An array of a subtype IS an array of its supertype to the type system: Animal[] animals = new Dog[3]; compiles, because Dog is an Animal, and arrays are covariant. Storing now asks a runtime question: animals[0] = new Cat(); fails with ArrayStoreException, because the actual object behind the reference was created as Dog[] and refuses Cat elements. The read side stays sound: animals[0] is always a Dog, hence an Animal.
This hole exists for Java 1.0's sake: without covariance, generic-ish methods like Arrays.sort(Object[]) and System.arraycopy could not be written. Generics later fixed the design for collections by refusing covariance entirely (List<Dog> is not a List<Animal>), leaving arrays as the one place a compile-time type allows a runtime type failure.
Why the details matter downstream
The header's element-type pointer is exactly what makes ArrayStoreException possible, and it is also what reflection reads: array.getClass().getComponentType(). System.arraycopy consults it per element and can throw the same way. Frameworks that copy and clone arrays generically lean on these rules.
The contiguity argument is what decides int[] versus ArrayList<Integer> in hot code: the primitives are packed four bytes apart with no per-element objects, the boxed list stores references to scattered Integers. Same algorithm, very different constants, and the arrays topic tomorrow's data-structure choices will keep referencing.
Covariance is compile-time; the element type is checked at store time
The runtime knows from the array header which element type it was created with, so every reference store into an Object-ish array slot re-checks it. That check is why List<Dog> to List<Animal> is banned (no re-check possible) while Dog[] to Animal[] is allowed (check happens per store).
Interview angles
Why is array covariance dangerous?+
It lets a Dog[] be seen as Animal[], and a later store of a Cat compiles but throws ArrayStoreException at runtime. Arrays gained generics-era safety never had; this is the one surviving compile-then-fail type hole, kept for 1.0-era APIs.
Why is length a field rather than a getter?+
It is stored in the object header and is final for the array's lifetime, so the JIT treats it as a constant. A getter would add nothing and block the optimiser's use of it, and changing it is impossible by design.
What does the JIT do with bounds checks?+
It proves most of them redundant. With an index that provably stays within 0 to length - 1 through the loop, the checks are hoisted or removed entirely. The safety net stays for cases the analysis cannot decide.