Scrivio Interview library
LeadershipPrincipal7 min read · updated 12 November 2026

Java has been moving toward immutable data with records, List.of, and value classes being explored in Project Valhalla. How do you guide a large codebase so today's design choices age well?

To make today's code age well, we model data as immutable values now, using records and value-based classes, which match the value classes Project Valhalla is exploring. We avoid depending on the identity of value-like objects: no synchronising on them, no == comparisons, no identity hash codes, since the JDK already warns about synchronising on value-based classes such as Integer. We keep constructors and factory methods stable as API so the representation can change later, and we do not adopt preview or early-access value features in production, planning to revisit when they are final.

Background to this question

Java has been moving steadily toward immutable data: records, unmodifiable collections, java.time, and now value classes in Project Valhalla, which may let the JVM store objects without identity as compactly as primitives. Decisions made today in a large codebase, how we model data and which object behaviours we rely on, will decide whether those future improvements are a free speed-up or a migration project. A senior leader is expected to guide teams so that the code is ready without betting on unfinished features. In this answer, we will look at what Valhalla is exploring, why identity is the key issue, the habits I would adopt now, how to keep APIs flexible, and how to treat preview features.

1A Quick Recap: What Valhalla Is Exploring

Every Java object today has identity: two objects with the same contents are still two distinct objects, == can tell them apart, we can synchronise on either, and each has its own header in memory. Identity is what costs memory and indirection: a List<Point> holds references to separate Point objects scattered around the heap.

Project Valhalla is exploring value classes (JEP 401, in early-access builds and preview proposals at the time of writing): classes whose instances have no identity. Without identity, the JVM is free to store them flat inside arrays and other objects and to copy them like primitives, which can greatly reduce memory and improve cache use. In exchange, operations that need identity, such as synchronising on the object, behave differently or fail. The details are still evolving, so check the current state of the JEPs rather than relying on any particular syntax.

2Why Identity Is the Key Issue

Code that treats a value-like object as a value, comparing it with equals and never locking on it, will keep working if that class becomes a value class, and may get faster. Code that depends on identity will break or change behaviour. That is the whole migration risk in one sentence.

The JDK has already started preparing. Since Java 16 (JEP 390), classes such as Integer, Double, Optional and the java.time types are documented as value-based, the constructors of the wrapper classes like new Integer(5) are deprecated for removal, javac warns when code synchronises on an instance of a value-based class, and the JVM can report such synchronisation at run time with a diagnostic flag.

Java
Integer lock = 42;
synchronized (lock) { } // javac: warning: [synchronization] attempt to synchronize on an instance of a value-based class
Integer a = 1000, b = 1000;
System.out.println(a == b); // false: two different boxes (only -128..127 are cached)
System.out.println(a.equals(b)); // true
LocalDate d1 = LocalDate.of(2026, 11, 4), d2 = LocalDate.of(2026, 11, 4);
System.out.println(d1 == d2); // false today; a value class would compare state, so never rely on it
Identity-dependent habits and their replacements
HabitRiskReplacement
synchronized on an Integer, String or recordshared or failing locksa private final Object lock or ReentrantLock
== on boxed numbers, dates, recordsresult depends on caching or identityequals or compareTo
System.identityHashCode or IdentityHashMap on valuesno stable identityhashCode and HashMap
new Integer(5), new Long(...)constructors deprecated for removalInteger.valueOf or autoboxing
WeakReference to value objectsbehaviour may changereference stable identity objects

Each line shows an identity dependence we should already avoid. Locking on an Integer shares the lock with any unrelated code that has the same cached box. Comparing with == gives answers that depend on caching. Under value classes, == on values is expected to compare their state, so code relying on either outcome would change meaning.

3Habits to Adopt Now

The first habit is to model data as immutable values. Records are the natural choice: they are final, their fields are final, and their equality is based on state, which is exactly the shape of a value class. Valhalla's proposals describe value records, so a record we write today is the most likely kind of class to benefit later with little or no change. Immutable final classes with state-based equals and hashCode, such as Money, are in the same position.

The second habit is to remove identity dependence on value-like types. Locks use dedicated lock objects. Comparisons use equals. Caches and maps rely on equals and hashCode, not on identity. These rules can be enforced today: the javac synchronization warning can be made an error with -Werror for that lint, and static analysis tools such as Error Prone flag reference equality on boxed types and other value-like classes.

Step throughIs this type ready to become a value class later?
  1. Is it immutable with state-based equals and hashCode?
    YescontinueNomake it a record or immutable final class
  2. Does any code synchronise on it or use ==?
    Yesreplace with lock objects and equalsNocontinue
  3. Is it created through a stable constructor or factory?
    Yesready: representation can changeNoadd factories before callers multiply

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

4Keep the API Stable So the Representation Can Change

The second part of aging well is to give ourselves room to change how a type is represented without breaking callers. Static factory methods such as Money.of(amount, currency) are the safest public entry point, because a factory can later return a cached instance, a value class or a more compact representation, and callers do not notice. Constructors and record components are also API: once many teams call new Money(...), its signature is fixed.

So for shared types, I would decide deliberately what is public. Expose the accessors and factories callers need, keep internal representation details out of the API, and avoid exposing the fact that something is a particular class if an interface or a factory would do. Then, when the platform offers a better representation, the change stays inside the library.

5Do Not Bet Production on Preview Features

Preview features in Java are complete designs that are not yet permanent: they need --enable-preview to compile and run, can change between releases, and can even be withdrawn. Early-access builds of Valhalla are further still from final. Using either in production code ties us to a specific JDK build and risks rewrites when the design changes, and it blocks routine JDK upgrades.

What I would do instead is run a small, time-boxed experiment in a platform team: take a hot data structure, try it with an early-access build, measure memory and speed, and report what the code needs to look like. That keeps the organisation informed and gives us a migration checklist, without putting unfinished features in front of customers. When value classes become final in an LTS release, we revisit, and if we followed the habits above, much of the code will need little or no change.

6Follow-up questions

Should we convert classes to records now just for Valhalla?

Not just for that. Convert when it improves the code today, for example when a class is a plain data carrier with hand-written equals and hashCode. The Valhalla benefit is a bonus. Converting public classes also changes their API, such as accessor names, so it is a versioned change for shared libraries.

How do we find code that synchronises on value-based objects?

Turn on javac's synchronization lint and treat it as an error in CI, and run with the JVM's diagnostic option for synchronisation on value-based classes in test environments to log any run-time cases; check your JDK version for the exact flag. Static analysis can also flag locks on String literals, boxed types and records.

Will value classes break code that uses == on records today?

Records are identity classes today, so == compares references. Code that relies on two equal records being different objects, or on the same reference being returned, could behave differently if those records became value records. That is why the rule is to compare value-like objects with equals everywhere, starting now.

7Conclusion

In this answer, we saw that Java is moving toward immutable values and that Project Valhalla is exploring value classes without identity, which could make such data much cheaper. The main risk for existing code is dependence on identity, so we model data as records and immutable values, avoid synchronising on or comparing value-like objects with ==, as the JDK already warns for value-based classes like Integer, and keep factories and constructors stable so the representation can change later. We experiment with preview and early-access builds in isolation, and adopt value classes only once they are final.

What to say in the interviewAbout 90 seconds aloud

Open with

I would guide teams with a few rules that pay off today and also prepare for value classes. First, model data as immutable values: records or immutable final classes with state-based equals and hashCode.

Then, if they want more

That is the shape Valhalla's value classes take, so these types are the ones most likely to benefit later, and they are better code now anyway. Second, never depend on the identity of value-like objects. No synchronising on Integer, String, records or java.time objects, no == comparisons on them, no identity hash codes.

The JDK is already preparing: since Java 16 the wrapper classes and others are documented as value-based, their constructors are deprecated for removal, and javac warns about synchronising on them, so I would make that warning an error in CI and add static analysis for reference comparisons.

Third, keep the public way of creating shared types stable, preferably through static factories like Money.of, because then the internal representation can change without callers noticing. Fourth, no preview or early-access value features in production; they need special flags, can change between releases and would block normal upgrades.

Instead, a platform team runs a small experiment with an early-access build on a hot data structure, measures it, and writes up what we would need to change. When value classes are final in an LTS release, we revisit, and code that followed these habits should need little work.

About this answer

Last updated
12 November 2026
Versions it names
Java 16
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 has been moving toward immutable data with records, List.of, and value classes...": https://scrivio-interview-library.netlify.app/java/immutability/f416ab555a/