Quick revision
The short answer
We prepare by making today's code easy to move later, not by using unfinished features. That means designing small data types as immutable, final and identity-free, such as records, and never using identity operations on them (==, synchronized, System.identityHashCode). It also means keeping generic APIs free of the assumption that every element is a nullable reference, limiting hand-written primitive specialisations to proven hot paths, and tracking the Valhalla JEPs with early-access test runs while keeping preview and early-access builds out of production.
What to remember
In this answer, we saw how to prepare a large codebase for Valhalla without depending on unfinished features. We design value candidates as immutable, final and identity-free types such as records, remove identity operations like ==, synchronized and identityHashCode from them, keep generic APIs free of the assumption that elements are nullable references, limit hand-written primitive specialisations to measured hot paths, and track the JEPs by testing early-access builds while keeping preview features out of production.
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.
We prepare by making today's code easy to move later, not by using unfinished features. That means designing small data types as immutable, final and identity-free, such as records, and never using identity operations on them (==, synchronized, System.identityHashCode). It also means keeping generic APIs free of the assumption that every element is a nullable reference, limiting hand-written primitive specialisations to proven hot paths, and tracking the Valhalla JEPs with early-access test runs while keeping preview and early-access builds out of production.
Why this question is asked
Valhalla is the OpenJDK project that will let the JVM store some objects as plain values, flattened into arrays and fields without object headers, and later let generic code work with them without boxing. A large codebase cannot wait for that, and it should not bet production on preview features either. Interviewers ask this design question to see whether we can separate what is certain (the direction) from what is not (dates and syntax), and turn it into habits that pay off today as well. In this answer, we will recap what value classes change, then go through the preparation steps: designing value candidates, removing identity dependencies, keeping generic APIs value-friendly, handling primitive specialisation, and running an early-access program.
1A Quick Recap of What Valhalla Changes
Every Java object today has identity: two objects with the same fields are still two different objects, and == tells them apart. Identity is what lets us lock on an object, change it while others hold a reference, and ask for its identity hash code. It is also why each object needs a header and lives separately on the heap, so an array of a million Point objects is really a million pointers to a million small objects.
A value class gives up identity. Its instances are defined only by their field values, so the JVM is free to copy them, store them flattened inside arrays and other objects, and keep them in registers. Valhalla's value classes are being delivered through JEPs that have been in preview or early access; generic specialisation, which would let a List of a value type avoid boxing, is planned for later. Check the current JEP status for your JDK version, because details such as syntax and null handling are still evolving.
| Aspect | Identity object (today) | Value object (Valhalla) |
|---|---|---|
| == compares | references | field values |
| synchronized (obj) | works | not allowed |
| Storage in an array | pointer to a separate object | can be flattened in place |
| Mutable fields | allowed | fields are final |
| Nullable references | always | may be restricted, check the JEP |
2Design Value Candidates as Immutable, Final and Identity-Free
The types that will benefit are the small data carriers we already have: Money, Point, OrderId, a date range, a measurement. If these are mutable classes with setters, or open for subclassing, they cannot become value classes without a breaking change. If they are records or final classes with final fields and equals based on fields, migration later may be close to a one-word change.
So our first rule is that new data carriers are records or final immutable classes, and existing ones are migrated when we touch them. This pays off today, too: immutable types are thread safe, safe as map keys and easier to reason about.
3Remove Every Dependency on Identity
Code that treats our value candidates as identities will either break or change behaviour when they become value classes. The three operations to hunt for are ==, synchronized and System.identityHashCode, plus related patterns such as IdentityHashMap keys and weak references used as identity tags.
We can find most of these mechanically. The JDK already warns about synchronizing on value-based classes such as Integer and Optional (javac has a synchronization lint, and the JVM has a diagnostic flag; check your version), and static analysis tools such as Error Prone and IntelliJ inspections flag reference comparison of objects with equals methods. We run these checks in CI on our value candidates now, so the code is clean long before we migrate.
4Keep Generic APIs Value-Friendly
Generic code often silently assumes that every T is a nullable reference. It uses null as a marker for a missing value, returns null from get when a key is absent, or stores null in an array slot to mean empty. If T may later be a value type that cannot be null, those APIs either cannot be specialised or need awkward workarounds.
So in new generic APIs we avoid null as a signal: return Optional<T> or provide a getOrDefault style method, model absence explicitly, and avoid methods whose contract is that a T parameter may be null. We also avoid relying on identity of T in generic code, for example using IdentityHashMap<T, ...> internally. None of this needs Valhalla to be useful; it simply makes the APIs cleaner.
5Do Not Specialise Everything by Hand
Boxing costs are real, and it is tempting to write IntList, LongList, IntIntMap and so on throughout the codebase. That creates large amounts of duplicated code that Valhalla will later make unnecessary. Our rule is to measure first: where a profiler shows boxing on a hot path, for example a pricing loop allocating 2 GB of Long objects per minute, we use primitive arrays, primitive streams or a well-known primitive collection library behind a small internal interface. Everywhere else we keep ordinary generic collections. That way, when specialisation arrives, there are only a few places to revisit.
6Track the JEPs and Test Early Without Shipping Preview Code
Finally, we treat Valhalla as a tracked dependency. A small group owns it: they follow the JEPs and the mailing list summaries, and they run our value candidate modules and benchmarks against early-access builds a few times a year. Running the test suite with preview features enabled shows which code breaks on identity or null assumptions and which hot paths actually get faster, which gives us real numbers instead of guesses.
What we do not do is depend on preview or early-access features in production. Preview features can change or be withdrawn between releases, and early-access builds are not supported. Production code stays on the current LTS release, Java 25 today, and is migrated to final features only once they are standard.
- Find value candidatessmall data carriers: Money,
OrderId, Point - Make them records or final immutablepays off today
- Remove identity uses==, synchronized,
identityHashCode - Clean generic APIsno null as a signal, no identity on T
- Specialise only proven hot pathsbehind small interfaces
- Test early-access builds regularlynever ship preview code
6 steps in order. Play them, or step through them one at a time.
7Follow-up questions
Should we start using the value keyword now?
Not in production. It is only available in early-access or preview builds, and its details can still change. We can try it in a branch or a benchmark project to learn, but production code should only use features that are final in the JDK release we run.
Are records automatically value classes?
No. A record today is still an identity class. Records are excellent candidates, because they are already final with final fields and value-based equals, but becoming a value class is a separate step that the type's owner will make once the feature is final.
What is the risk of ignoring all this?
Mostly missed opportunity rather than breakage, because existing identity classes will keep working. The cost appears when we want the performance benefit and find our hot data types are mutable, subclassed or locked on, so they cannot be migrated without a large refactoring.
8Conclusion
In this answer, we saw how to prepare a large codebase for Valhalla without depending on unfinished features. We design value candidates as immutable, final and identity-free types such as records, remove identity operations like ==, synchronized and identityHashCode from them, keep generic APIs free of the assumption that elements are nullable references, limit hand-written primitive specialisations to measured hot paths, and track the JEPs by testing early-access builds while keeping preview features out of production.
Open with
I would prepare in ways that are useful today and do not depend on anything unfinished. First, I would identify our value candidates, the small data carriers like Money, OrderId or Point, and make them records or final immutable classes with value-based equals.
Then, if they want more
That is good design anyway, and it is what value classes will need. Second, I would remove identity dependencies on those types: no == comparisons, no synchronizing on them, no identityHashCode or IdentityHashMap, and I would add static analysis to CI so new ones do not creep in.
Third, I would keep new generic APIs value-friendly, which mostly means not using null as a signal in a T position; return Optional or provide getOrDefault, so the API could later work with non-nullable value types.
Fourth, I would avoid writing primitive-specialised collections everywhere; only where profiling shows boxing on a hot path, and behind a small interface, so there is little to undo when specialisation arrives.
Finally, I would have a small group track the Valhalla JEPs and run our tests and benchmarks against early-access builds a few times a year, but production stays on the current LTS and never on preview features.
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 "If Project Valhalla delivers value classes and later generic specialisation, collections...": https://scrivio-interview-library.netlify.app/java/generics/3a0460d521/