Quick revision
The short answer
In HotSpot an array is an object with a header, a mark word and a class pointer, then a length field, followed by its elements stored one after another. An int[] stores the int values themselves in that contiguous block, while an Integer[] stores only references, each pointing to a separate Integer object somewhere else on the heap, so iterating int[] reads memory sequentially, uses far less of it, and lets the JIT remove bounds checks and use SIMD instructions, while Integer[] adds a pointer dereference, a likely cache miss and an unboxing step for every element.
What to remember
In this answer, we saw that a HotSpot array is an object with a mark word, a class pointer and a length, about 16 bytes in total, followed by contiguous elements. An int[] stores values directly, while an Integer[] stores 4-byte compressed references to separate 16-byte Integer objects, about five times the memory. Sequential primitive data fits cache lines and the prefetcher, and lets C2 remove bounds checks in counted loops and vectorize with SIMD, while boxed elements need an extra load, a likely cache miss and unboxing. References are 4 bytes only below about 32 GB of heap, and JMH and JOL let us measure all of this.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
In HotSpot an array is an object with a header, a mark word and a class pointer, then a length field, followed by its elements stored one after another. An int[] stores the int values themselves in that contiguous block, while an Integer[] stores only references, each pointing to a separate Integer object somewhere else on the heap, so iterating int[] reads memory sequentially, uses far less of it, and lets the JIT remove bounds checks and use SIMD instructions, while Integer[] adds a pointer dereference, a likely cache miss and an unboxing step for every element.
Why this question is asked
Performance problems in Java services often come down to memory layout rather than algorithms. Two loops with the same big-O complexity can differ by a factor of five or more because one walks through memory in order and the other jumps around it. Interviewers ask about array layout to see whether we can reason about what the hardware is doing, which explains why primitive arrays and specialised collections exist at all. In this answer, we will look at how an array is laid out, compare int[] and Integer[] byte by byte, see why the CPU cache and the JIT favour the primitive array, and finish with compressed references and how to measure all this properly.
1How an Array Is Laid Out
Every Java object in HotSpot starts with a header. The first part is the mark word, 8 bytes on a 64-bit JVM, which holds the identity hash code, lock state and garbage collector information. The second is the class pointer, which says what type the object is; with compressed class pointers, the default, it takes 4 bytes. An array adds one more field, its length, as a 4-byte int. After that come the elements, packed together with no gaps, and the whole object is padded to a multiple of 8 bytes.
So the address of a[i] is simply the array's address plus 16 plus 4 times i. That is why array access is so fast: it is one multiplication, one addition and one load. JDK 25 also offers compact object headers as an opt-in feature, -XX:+UseCompactObjectHeaders, which merges the class pointer into an 8-byte header and shrinks every object, so the exact numbers here depend on the flags and version (check your version).
2Int[] Versus Integer[] in Memory
An Integer[] uses the same array layout, but its elements are references, 4 bytes each with compressed references, and each reference points to an Integer object. An Integer object has its own 12-byte header and a 4-byte int value, so it takes 16 bytes. Integer.valueOf caches the values from -128 to 127, so small numbers share objects, but most real data falls outside that range.
| Layout | Array object | Separate objects | Total |
|---|---|---|---|
| int[] | 16 B + 4 MB of values | none | about 4 MB |
| Integer[] | 16 B + 4 MB of references | 1,000,000 Integers x 16 B | about 20 MB |
| Integer[], heap over 32 GB | 16 B + 8 MB of references | 1,000,000 x 16 B or more | about 24 MB |
3Why Iterating int[] Is Faster
A modern CPU reads memory in cache lines of 64 bytes and keeps recently used lines in small, fast caches. A line of an int[] holds 16 consecutive values, so summing the array uses every byte the CPU fetches. The hardware prefetcher also notices the sequential pattern and loads the next lines before the loop asks for them, so the loop rarely waits for main memory.
The boxed loop does two loads per element: the reference from the array, then the value inside the Integer object it points to. Those objects may be scattered across the heap, depending on how and when they were allocated and how the garbage collector moved them, so each one may be a cache miss costing on the order of 100 nanoseconds. Even when they happen to lie close together, the loop touches five times as much memory. Notice that a[i] in the boxed loop also unboxes, and a null element would throw NullPointerException there.
4What the JIT Can Do With a Primitive Array
Java checks every array access against the length and throws ArrayIndexOutOfBoundsException when the index is out of range. Doing that on every iteration would be expensive, so the C2 JIT compiler analyses simple counted loops, such as i from 0 while i < a.length, proves the index is always in range and removes the checks from the loop body.
Once the loop is a plain sequence of loads and adds over contiguous ints, C2 can also vectorize it, using SIMD instructions that process 4, 8 or more ints in a single instruction, an optimisation HotSpot calls superword. That is impossible for Integer[], because the values are not next to each other: each one must be fetched through its own reference. So the primitive loop benefits twice, from better memory access and from wider instructions.
- Is the array an int[]?YesLoad the int at base + 16 + 4 x iNoLoad the reference at base + 16 + 4 x i
- Follow the reference to the Integer objecta second load, often a cache miss
- Read the value field and unboxthrows if the element is null
3 steps in order. Play them, or step through them one at a time.
The primitive array needs one load; the boxed array needs two, at unrelated addresses.
5Compressed References and the 32 GB Boundary
On a 64-bit JVM a reference could be 8 bytes, but HotSpot uses compressed oops, ordinary object pointers stored in 4 bytes, whenever the heap is small enough. Objects are aligned to 8 bytes, so a 32-bit value can address 2^32 x 8 bytes, which is 32 GB. With a heap below roughly 32 GB, references are 4 bytes; above it, compression is turned off and every reference becomes 8 bytes. That is why a JVM with a 33 GB heap can hold less data than one with 31 GB, and why the boundary matters so much for reference-heavy structures like Integer[].
6Measuring It Properly
Claims like several times slower should be measured, and in Java that means JMH, the benchmark harness from the OpenJDK project, which handles warm-up, JIT compilation and dead-code elimination that make hand-written timing loops misleading. To see the actual layout of objects on our JVM, with our flags, the JOL tool, Java Object Layout, prints the offset and size of every field and the header, so we can confirm numbers such as the 16-byte array header instead of trusting a table. For production data, a heap histogram from jcmd <pid> GC.class_histogram shows how much memory goes to java.lang.Integer objects, which is often the first sign that boxed collections are the problem.
7Follow-up questions
Does the Integer cache make Integer[] as compact as int[]?
No. Integer.valueOf, which autoboxing uses, returns cached objects only for values from -128 to 127 by default, so an array of small numbers shares those objects and saves their memory. But the array still holds references rather than values, every access still follows a reference and unboxes, and most real data, ids, prices, timestamps, is outside the cache range. The cache helps memory a little in special cases and does nothing for the access pattern.
What can we use when we need a collection of primitives?
Plain int[] or long[] arrays are the simplest. Streams have IntStream and LongStream to avoid boxing. For growable lists and maps of primitives, libraries such as Eclipse Collections, fastutil or HPPC offer IntArrayList or IntIntHashMap, which keep values in primitive arrays internally. In a hot path, replacing a List<Integer> or Map<Long, Long> with one of these often cuts memory by several times and speeds up iteration accordingly.
Will Project Valhalla change the Integer[] picture?
That is one of its goals. Value classes, objects without identity, would allow the JVM to store their fields directly inside arrays, flattening them like primitives, and the plan includes treating Integer and friends this way. As of Java 25, value classes are still in early-access builds and not part of a release, so today Integer[] is laid out exactly as described here, and code that needs dense numeric data should use primitive arrays.
8Conclusion
In this answer, we saw that a HotSpot array is an object with a mark word, a class pointer and a length, about 16 bytes in total, followed by contiguous elements. An int[] stores values directly, while an Integer[] stores 4-byte compressed references to separate 16-byte Integer objects, about five times the memory. Sequential primitive data fits cache lines and the prefetcher, and lets C2 remove bounds checks in counted loops and vectorize with SIMD, while boxed elements need an extra load, a likely cache miss and unboxing. References are 4 bytes only below about 32 GB of heap, and JMH and JOL let us measure all of this.
Open with
In HotSpot an array is an object: an 8-byte mark word for hash, locking and GC bits, a class pointer, 4 bytes with compressed class pointers, and a 4-byte length, so a 16-byte header, followed by the elements stored contiguously, padded to 8 bytes.
Then, if they want more
Element i is at base plus 16 plus element size times i, which is why access is so cheap. An int array stores the values themselves.
An Integer array stores references, 4 bytes each with compressed oops, pointing to separate Integer objects of about 16 bytes each, scattered on the heap, so a million values take around 20 megabytes instead of 4. Iteration is faster for int arrays for two reasons.
First, the hardware: a 64-byte cache line holds 16 consecutive ints, and the prefetcher sees the sequential pattern, so the loop barely waits for memory, while the boxed loop loads a reference, then follows it to another address, often a cache miss, and unboxes.
Second, the JIT: every array access is bounds-checked, but C2 proves simple counted loops stay in range and removes the checks, and then it can vectorize the loop with SIMD instructions, which is impossible when values are behind references.
One more detail: references are only 4 bytes while the heap is under about 32 gigabytes, because objects are 8-byte aligned; above that every reference doubles. I'd confirm numbers with JOL for layout and JMH for timing rather than guessing.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 25
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "How is an array laid out in HotSpot memory, and why is iterating an int[] typically much...": https://scrivio-interview-library.netlify.app/java/arrays/8815b51641/