Scrivio Interview library
LeadershipPrincipal6 min read · updated 12 November 2026

Java's ArrayList stores references to boxed objects, unlike arrays of primitives or value types in other languages. How does Project Valhalla's work on value classes relate to this, and how should you plan data-heavy code today?

A List<Integer> stores references to separate Integer objects, so a million ints take around 20 MB instead of the 4 MB of an int[]. Project Valhalla's value classes aim to let objects without identity be stored flat, and later to let generics work over them, but the timeline is not fixed. Today we use primitive arrays or primitive collection libraries for large numeric data, and we write value-like types as immutable records that never depend on identity, so they can benefit when the feature arrives.

Why this question is asked

Senior engineers are expected to plan beyond the current release: which data structures will scale, and which designs will need a rewrite when the platform changes. Valhalla is the largest change to Java's memory model in decades, and interviewers ask about it to see whether we understand the cost of boxing, follow the platform's direction without betting on dates, and can turn that into practical guidance for a team. In this answer, we will recap how generic collections hold data, see the memory cost, explain what Valhalla changes, and then set out how we would plan data-heavy code today.

1A Quick Recap: Collections Hold References

Java has two kinds of values. Primitives, such as int and double, are stored directly where they are declared. Objects live on the heap and are reached through references. Generics only work with objects, so a List<Integer> cannot hold ints. Each value is boxed into an Integer object, and the list's internal array holds references to those objects.

Every object also has identity: it is a distinct thing in memory, which is why == can tell two equal Integers apart and why we can synchronise on an object. Identity forces the JVM to give each object its own header and its own place on the heap.

Java
int[] raw = new int[1_000_000]; // one object: about 4 MB of ints in a row
List<Integer> boxed = new ArrayList<>(1_000_000);
for (int i = 0; i < 1_000_000; i++) boxed.add(i);
// about 4 MB of references plus about 16 MB of Integer objects (small values -128..127 are cached)
Integer a = 1000, b = 1000;
System.out.println(a == b); // false: two objects with separate identity
System.out.println(a.equals(b)); // true
Step throughMemory layout of int[] versus List<Integer>
  1. int[]values stored inline: 1, 2, 3 ... one contiguous block
  2. ArrayList<Integer>small object pointing to an Object[]
  3. Object[] elementDatareferences only, 4 bytes each
  4. Integer objectsone per value, 16 bytes each, scattered on the heap

4 layers, from the top down. Step through them one at a time.

Each extra layer is extra memory and an extra pointer to follow.

So the boxed list uses about five times the memory of the array, and iterating it chases a pointer per element to wherever each Integer happens to live. That second cost often hurts more than the first, because the CPU cache works best on data laid out in a row.

2What Valhalla Is Trying to Change

Project Valhalla is the OpenJDK project working on value classes. A value class declares that its instances have no identity: two instances with the same fields are simply the same value, like two ints with the same bits. Without identity, the JVM is free to store the fields flat, inside an array or inside another object, with no separate header or pointer, much like a primitive.

The plan is staged. The first part, value classes and objects, has been developed as a JEP with early-access builds and preview work; as of Java 25 it is not a final feature, so we check the current JDK release notes for its status. A later part aims to let generics work over value types and primitives, so a List of a value type could be backed by flat storage. That part is further away, and nobody can promise a release. The boxed classes such as Integer are expected to become value classes, which is why the JDK already warns when code synchronises on them.

3Planning Data-Heavy Code Today

We should not wait for a feature without a date. For large numeric data today, we use what the platform already offers: primitive arrays for fixed-size data, and primitive collection libraries such as fastutil or Eclipse Collections, which provide IntArrayList and similar types that grow like ArrayList but store ints. Streams have IntStream, LongStream and DoubleStream for the same reason.

Java
// A large numeric column: primitives today
int[] scores = loadScores(); // flat, 4 bytes each
IntStream.of(scores).average().orElse(0); // no boxing
// A value-like domain type: a record that never relies on identity
record Money(long cents, String currency) {}
Money a = new Money(500, "EUR"), b = new Money(500, "EUR");
System.out.println(a.equals(b)); // true: compare by value, never with ==

For domain types that behave like values, such as Money, a coordinate or a date range, we write immutable records and treat them strictly by value. A record already has final fields and value-based equals() and hashCode(), which is the shape a value class needs. Whether a given record can become a value class later depends on the final rules of the feature, so we do not promise migration, but we make it possible.

4Designs That Would Need Rewrites

The risk is code that depends on identity, because identity is exactly what value classes give up. Synchronising on a value-like object, comparing such objects with ==, using them in an IdentityHashMap, or relying on System.identityHashCode all assume that each instance is a distinct thing. They work today and would break, or be rejected by the compiler, once those types become value classes.

So the guidance we would give a team is concrete. Compare value-like types with equals(), never ==. Lock on a dedicated private lock object, never on a domain value or a boxed number. Keep numeric hot paths behind a small interface, so that switching from long[] today to a flat List of a value type later is a local change. And measure: boxing matters for millions of elements in a hot path, not for a list of twenty configuration values.

Step throughChoosing a representation for a data-heavy structure
  1. Is the data numeric and large?millions of elements, hot path
    YesPrimitive array or primitive collectionNoIs it a value-like domain type?
  2. Is it a value-like domain type?
    YesImmutable record, no identity useNoOrdinary class
  3. Hide the representationsmall interface around hot paths
  4. Review when value classes shipmeasure before migrating

4 steps in order. Play them, or step through them one at a time.

5Follow-up questions

Does the Integer cache solve the boxing cost?

Only for small values. Integer.valueOf caches -128 to 127 by default, so those boxes are shared, but every other value gets its own object. The cache also makes == appear to work in tests with small numbers and then fail in production with large ones, which is one more reason never to compare boxes with ==.

Will existing code using List<Integer> become faster automatically?

Possibly in part, but we should not plan on it. Flattening inside generic collections needs the later generics work, which has no fixed release. Code that keeps identity out of value-like types is best placed to benefit, and code that is too slow today should be fixed today with primitives.

Are records the same thing as value classes?

No. A record is about a transparent, immutable data carrier, and it still has identity today: two equal records can be different objects. A value class is about giving up identity so the JVM can flatten it. The two ideas fit together, and a record may be declared as a value record if the final feature allows it, but they are different features.

6Conclusion

In this answer, we saw that generic collections hold references to boxed objects, so a List<Integer> costs about five times the memory of an int[] and adds a pointer hop per element. Valhalla's value classes aim to remove identity so the JVM can store such values flat, with generic support planned later and no fixed timeline. Today we use primitive arrays and primitive collection libraries for large numeric data, write value-like types as immutable records compared with equals(), and avoid identity-dependent designs that would need rewrites.

What to say in the interviewAbout 90 seconds aloud

Open with

Java generics only work with references, so a List of Integer stores a reference per element to a separate Integer object. A million ints in an int array take about four megabytes in one block.

Then, if they want more

In an ArrayList they take about four megabytes of references plus roughly sixteen of Integer objects scattered on the heap, and every access follows a pointer, which hurts cache locality more than the raw memory.

Project Valhalla is working on value classes, objects that give up identity so the JVM can store their fields flat, like primitives, and later on letting generics work over them so collections could be flat too.

The first part has been in preview and early-access work, and the generics part has no fixed date, so I would not plan around a release. For data-heavy code today, I use primitive arrays, IntStream, or primitive collection libraries like fastutil or Eclipse Collections for large numeric data.

For value-like domain types, I write immutable records and make sure nobody depends on their identity: compare with equals and never with double equals, never synchronise on them, no IdentityHashMap. Those identity-based designs are exactly what would need rewriting when value classes arrive.

I also keep hot numeric paths behind a small interface so the representation can change locally, and I only optimise where measurements show boxing matters.

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.
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 "Java's ArrayList stores references to boxed objects, unlike arrays of primitives or...": https://scrivio-interview-library.netlify.app/java/lists/3550e2fbfa/