Quick revision
The short answer
I would standardise on hiding the data representation behind small interfaces rather than standardising on arrays or on collections, because arrays are still the most efficient on-heap layout for numeric data and the JIT optimises them well, while the platform is changing underneath us: the Foreign Function and Memory API has been final since Java 22 and replaces sun.misc.Unsafe for off-heap memory, the Vector API is still incubating, and value classes are not part of Java 25. So arrays stay allowed in proven, benchmarked hot paths behind those interfaces, collections are the default elsewhere, and value types are written today as immutable, identity-free records so they can move to value classes later.
What to remember
In this answer, we weighed arrays, which remain the most efficient on-heap layout and are well optimised by the JIT, against collections, which make ownership and safety explicit. The FFM API is final since Java 22 and is the supported replacement for Unsafe off-heap memory, while the Vector API is still incubating and value classes are not in Java 25. So we standardise small interfaces that hide the representation, allow arrays in benchmarked hot paths with ownership rules, use FFM for off-heap data, keep value types immutable and identity-free, and revisit the decision at each LTS release.
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.
I would standardise on hiding the data representation behind small interfaces rather than standardising on arrays or on collections, because arrays are still the most efficient on-heap layout for numeric data and the JIT optimises them well, while the platform is changing underneath us: the Foreign Function and Memory API has been final since Java 22 and replaces sun.misc.Unsafe for off-heap memory, the Vector API is still incubating, and value classes are not part of Java 25. So arrays stay allowed in proven, benchmarked hot paths behind those interfaces, collections are the default elsewhere, and value types are written today as immutable, identity-free records so they can move to value classes later.
Why this question is asked
Every large Java organization ends up with two cultures: the teams who measure nanoseconds and pass raw double[] buffers between components, and the teams who value readable, safe code with List<Double>. Both are right about something, and the platform is about to change the trade-offs with off-heap memory, vector instructions and value classes. Interviewers ask this leadership question to see whether we can make a decision that holds for years without betting on features that are not ready. In this answer, we will look at what arrays still do best, the real status of the three new platform pieces, the principle I would standardise on, where arrays are allowed, and how to prepare for value classes.
1The Tension Between the Two Camps
The performance teams have a point. A double[] stores values contiguously, so loops are cache-friendly, the JIT removes bounds checks and can vectorize them, and there is no boxing. A List<Double> of the same million values uses around five times the memory and is several times slower to iterate. In a pricing engine or a telemetry pipeline that difference is the difference between meeting a latency target and missing it.
The other teams have a point too. Arrays are mutable and have no encapsulation: a component that receives a double[] can change it, keep a reference and read it later after the sender has reused it. Bugs from shared arrays are hard to find because nothing in the type tells us who owns the data. Collections, especially immutable ones, make ownership and intent explicit. The question for a leader is how to get the first benefit where it matters without paying the second cost everywhere.
2Where the New Platform Pieces Stand
Three platform developments touch this decision, and they are at very different stages, so we must not treat them alike.
| Feature | Status in Java 25 | What it means for us |
|---|---|---|
| Foreign Function and Memory API | Final since Java 22 | Supported off-heap memory; replace Unsafe and ByteBuffer hacks |
| Vector API | Still an incubator module | Experiments only; needs --add-modules; may change |
| Value classes (Valhalla) | Not in a release; early access | Do not design APIs around them yet |
| Plain arrays + JIT | Mature | Auto-vectorization of simple loops already happens |
3What Each Feature Changes
The FFM API, in java.lang.foreign, gives us MemorySegment and Arena: memory outside the heap, with 64-bit sizes, bounds checks, and deterministic release when the arena closes. Teams that use sun.misc.Unsafe or very large direct ByteBuffers for off-heap data, for example for memory-mapped files or data shared with native libraries, now have a supported replacement, and the JDK is restricting Unsafe's memory methods, so migrating them is a standard we can set today.
The Vector API lets us write explicit SIMD code that the JIT turns into vector instructions on each CPU. It is powerful for numeric kernels, but it has been incubating for many releases and is still an incubator module in Java 25, waiting for value classes before it can become final, so it should not become a dependency of shared code (check your version). Value classes would let the JVM flatten objects without identity into arrays, closing much of the gap between double[] and a list of small value objects. They are the biggest change, and they are not in a release yet, so any plan that depends on them is a bet on timing.
4The Principle: Standardise the Interface, Not the Representation
Because the best representation will change over the next few years, the thing to standardise is a small interface that hides it. Components exchange a type that describes what the data is, and only the implementation knows whether it is an array, a memory segment or, later, a flattened array of value objects.
The JIT inlines small methods like priceAt in hot loops, so the interface usually costs nothing measurable, and the bulk copyTo method covers the cases where it does. The array never escapes, which removes the shared-mutation bugs, and swapping the implementation later is a local change instead of a migration across every team.
5Where Raw Arrays Are Allowed
Inside a component, arrays are fine wherever they help. Across component boundaries, the rule is collections or the small interfaces by default, and raw arrays only on hot paths where a JMH benchmark shows the difference matters, recorded in an architecture decision record. Those paths follow an ownership convention, either the receiver copies, or the API is named as a transfer of ownership so the sender never touches the array again. Where teams need growable primitive collections, we approve one library, such as fastutil or Eclipse Collections, rather than letting each team pick its own.
- Is this path hot, with a benchmark showing it matters?YesArrays allowed, behind an interfaceNoUse collections or a small interface
- Is the data off-heap or shared with native code?YesUse the FFM API: MemorySegment and ArenaNoStay on-heap
- Does anything depend on Vector API or value classes?YesExperiment only; keep it out of shared codeNoShip it
3 steps in order. Play them, or step through them one at a time.
6Preparing for Value Classes Without Betting on Them
We can make the future migration cheap with habits that are good today anyway. Small data types, such as a Price or a Coordinate, are written as records: immutable, with equals based on their fields. Code never relies on their identity: no == comparisons, no synchronising on them, no use of System.identityHashCode, no weak references to them. A type that follows these rules is exactly what a value class needs, so when value classes arrive, turning such a record into a value record should be close to a one-word change, and arrays of them could then be flattened by the JVM.
Finally, the decision itself has an expiry date. We review it at each LTS release, with benchmarks that compare the current implementations against the new platform features, so that when the Vector API or value classes become final we adopt them through the interfaces, on evidence, rather than through a rewrite.
7Follow-up questions
Why not adopt the Vector API now if it gives large speedups?
Because an incubator module can change incompatibly from one release to the next, needs --add-modules at compile and run time, and ties every consumer to that choice. In a single, isolated numeric kernel with an owner who accepts upgrade work, an experiment can be worth it, measured with JMH against the auto-vectorized array loop, which is often closer than people expect. In shared libraries or across component boundaries, it would turn every JDK upgrade into a coordinated migration. We wait for it to become final, and the interfaces mean adopting it later is a local change.
What does the FFM API give over direct ByteBuffers?
MemorySegment supports 64-bit sizes, while a ByteBuffer is limited to about 2 GB because it uses int indexes. Arena gives deterministic release of off-heap memory when it is closed, while direct buffers are freed only when the garbage collector notices them, which can lead to native memory pressure. Segments also check bounds and confinement, so use-after-free and wrong-thread access raise exceptions instead of crashing the JVM, and memory layouts describe structured data. Together they replace most uses of Unsafe and ByteBuffer for off-heap work.
How do you stop arrays from leaking mutability between components?
By not letting them cross boundaries naked. The receiving side copies an array it stores, with clone or Arrays.copyOf, and the giving side never returns its internal array, only a copy or a read-only view such as a small interface. Where copies are too expensive, the API name and documentation state a transfer of ownership, and code review and ArchUnit rules check that public methods of shared modules do not take or return raw arrays outside the approved hot paths.
8Conclusion
In this answer, we weighed arrays, which remain the most efficient on-heap layout and are well optimised by the JIT, against collections, which make ownership and safety explicit. The FFM API is final since Java 22 and is the supported replacement for Unsafe off-heap memory, while the Vector API is still incubating and value classes are not in Java 25. So we standardise small interfaces that hide the representation, allow arrays in benchmarked hot paths with ownership rules, use FFM for off-heap data, keep value types immutable and identity-free, and revisit the decision at each LTS release.
Open with
I'd avoid standardising on either arrays or collections, and instead standardise on hiding the representation behind small interfaces. Arrays are still the most efficient on-heap layout for numeric data: contiguous, no boxing, and the JIT removes bounds checks and auto-vectorizes simple loops, so in a real hot path they can be several times faster than List of Double.
Then, if they want more
But they're mutable and unencapsulated, so sharing them across components causes ownership bugs. Looking ahead, the three features are at very different stages. The Foreign Function and Memory API has been final since Java 22, so for off-heap data I'd standardise on MemorySegment and Arena and migrate away from Unsafe and big direct ByteBuffers.
The Vector API is still an incubator module, so it's for experiments, not shared code. Value classes aren't in Java 25, so I wouldn't design APIs around them.
So the rule would be: components exchange small interfaces, like a PriceSeries with size, get and a bulk copy method, backed by an array today and possibly a memory segment or flattened value objects later, without callers changing.
Raw arrays are allowed in proven hot paths, with a JMH benchmark and an ownership convention, and we pick one primitive collections library. To prepare for Valhalla, small value types are immutable records that never rely on identity: no ==, no synchronising on them. And we revisit the decision each LTS release with benchmarks.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 25, Java 22
- 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 "Your organization's high-throughput services still pass large primitive arrays between...": https://scrivio-interview-library.netlify.app/java/arrays/a124d1e3f2/