Scrivio Interview library
ConceptPrincipal7 min read · updated 12 November 2026

Project Valhalla proposes value classes whose instances have no identity. Explain why Java originally gave every object identity, what that has cost, and how you would prepare a large codebase.

Java gave every object identity so that locking, mutation and reference equality would work the same way for every object, but the price is a header on each object and, usually, a pointer to reach it, which stops the JVM from packing small values tightly into arrays and fields. Valhalla's value classes give up identity, so == compares state and synchronization is not allowed, and in return the JVM may flatten them. We prepare a large codebase by already treating value-based classes as identity-free and by keeping our own small value types immutable, final and compared by value, ideally as records.

Why this question is asked

For thirty years, every Java object has been a little island on the heap with its own header and its own address. That uniform model is simple to reason about, and it is also the main reason a List of a million small points uses several times more memory than the same data in C. Interviewers ask senior engineers about Project Valhalla to see whether they understand that trade-off and can get a large codebase ready for a change that alters the meaning of == for some types. In this answer, we will recap what identity gives us, measure what it costs, see what value classes change, and then go through the concrete steps to prepare.

1A Quick Recap: What Identity Gives Us

Identity means an object is one particular instance, distinguishable from every other even when their fields are equal. Several language features are built on it. Reference equality with == asks whether two references point to the same instance. synchronized locks a specific instance, so the lock lives with the object. Mutation is visible to everyone who holds a reference, because they all share the same instance. And System.identityHashCode() returns a hash tied to the instance.

Early Java made all of this uniform: every object, however small, has identity. A Point with two ints and a giant service object follow the same rules. That uniformity was a sensible choice in 1995, because it kept the language and the JVM simple.

2What Identity Costs in Memory and Speed

To support identity, every object carries a header. On a 64-bit HotSpot JVM with compressed class pointers the header is typically 12 bytes, and objects are aligned to 8 bytes. Java 25 made compact object headers (JEP 519) a product option that shrinks it to 8 bytes, but there is still a header. And because each object is a separate instance, an array or a field of that type holds a reference, not the data itself.

Java
record Point(int x, int y) {}
Point[] points = new Point[1_000_000];
for (int i = 0; i < points.length; i++) points[i] = new Point(i, i);
// Each Point: 12-byte header + 8 bytes of ints = 20, padded to 24 bytes -> 24 MB
// The array: 1,000,000 compressed references of 4 bytes -> 4 MB
// Total about 28 MB for 8 MB of actual data (two ints x 1,000,000)
int[] xs = new int[1_000_000], ys = new int[1_000_000]; // what we write by hand to get 8 MB
Point[] today and with a flattened value class
Identity objectsFlattened values
Array holdsreferences to separate objectsthe x and y fields inline
Memory for 1M pointsabout 28 MBabout 8 MB, plus any null marker
Reading element iload reference, then follow itread consecutive memory
== comparesreferencesfield values
synchronized(p)allowednot allowed
Flattening is permitted, not guaranteed: the JVM decides based on size, nullability and atomicity.

The memory overhead is only half of the cost. When we loop over the array, each element is a pointer to somewhere else on the heap, so the processor follows a pointer for every point, and the points may be scattered. Modern CPUs are fast at reading consecutive memory and slow at chasing pointers, so this hurts cache locality. Performance-minded Java code has long worked around it with parallel primitive arrays, which are fast but awkward and lose the type safety of a Point.

3What Value Classes Change

Project Valhalla proposes value classes, described in JEP 401 (Value Classes and Objects), which at the time of writing is a preview feature in early-access builds rather than part of Java 25; check the status for your JDK. A value class is declared with the value modifier, its fields are final, and its instances have no identity.

Java
value record Point(int x, int y) {} // preview syntax, check your JDK
Point a = new Point(1, 2);
Point b = new Point(1, 2);
System.out.println(a == b); // true: == compares the field values
synchronized (a) { } // compile error, or IdentityException at run time

Because there is no identity, == can no longer ask whether two references are the same instance, so it compares the objects' fields instead. Synchronizing on a value object is an error, because there is no instance to own a lock. In exchange, the JVM is free to copy the values around, store them in registers, and flatten them directly into arrays and fields, giving the memory layout of the parallel int arrays with the readability of a Point.

The JDK has prepared for this for years. Classes such as Integer, Long, Optional, LocalDate and the other java.time types are documented as value-based: their equality is by value and code must not rely on their identity. Since JDK 16 (JEP 390), the wrapper constructors such as new Integer(5) are deprecated for removal, and javac warns when we synchronize on a value-based class. These are the classes most likely to become value classes.

4Preparing a Large Codebase

The preparation is mostly about removing code that depends on the identity of value-based objects, because that code will change meaning or fail. There are three habits to hunt. The first is == between value-based objects, such as two Integers or two LocalDates, which today may be false for equal values and tomorrow may be true. It is a bug in both worlds, so we replace it with equals(). The second is synchronizing on them, for example a lock on an Integer from a map. The third is relying on identityHashCode or IdentityHashMap with such keys.

Java
// Find and fix before the change, not after
if (order.getAmount() == other.getAmount()) { } // Integer ==: replace with equals()
synchronized (userIdLocks.get(userId)) { } // lock on an Integer: use a dedicated lock object
Integer count = new Integer(5); // deprecated for removal: use Integer.valueOf(5)
// Compiler and tools help
// javac -Xlint:synchronization warns when synchronizing on value-based classes
// javac -Xlint:removal flags new Integer(...) and friends
Step throughGetting a codebase ready for value classes
  1. Enable -Xlint:synchronization and -Xlint:removaltreat the warnings as errors
  2. Replace == on wrappers, Optional, java.timeuse equals()
  3. Replace locks on value-based objectsprivate final Object or ReentrantLock
  4. Model small domain values as recordsimmutable, final, value equality
  5. Add a static analysis rulekeeps new code clean

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

For our own types, we design small domain values so that they could become value classes later without any caller noticing. That means immutable state, a final class, equality and hash code based on the fields, and no use of the instance as a lock. Records already give us almost all of that by construction: they are final, their fields are final, and equals() and hashCode() compare components. A Money, an OrderId or a Coordinate written as a record today is a natural candidate for a value record later.

We also bake the rules into tooling so they hold across hundreds of modules: compiler lint flags turned into errors in the shared build, a static analysis rule against == on value-based and record types, and an architecture test that forbids synchronized blocks on anything other than private final lock objects. This is cheap now and turns the eventual migration into a recompile rather than a hunt.

5Follow-up questions

Will existing code break when Integer becomes a value class?

Correct code will not. Code that compares Integers with == may change behaviour, and code that synchronizes on an Integer will fail. Both were already wrong, because they relied on caching and identity details the JDK never promised. That is why the JDK has been warning about them since JDK 16.

Can a value class field be null, and does that affect flattening?

A variable of a value class type can still be null in the JEP 401 design, and the JVM must represent that, which may need an extra marker and can limit flattening. Later Valhalla work on null-restricted types aims to let us declare that a field or array element is never null so it can be flattened fully. Details are still evolving, so check the current JEPs.

6Conclusion

In this answer, we saw that Java gave every object identity to make locking, mutation and == uniform, at the cost of a header per object and a reference per field or array slot, which wastes memory and cache locality for small values. Valhalla's value classes give up identity, so == compares state and synchronization is not allowed, and the JVM may flatten them. To prepare, we stop relying on the identity of value-based classes such as the wrappers, Optional and java.time types, and we model our own small values as immutable records with value equality.

What to say in the interviewAbout 80 seconds aloud

Open with

Java gave every object identity so the language could be uniform: any object can be compared with ==, locked with synchronized, mutated and shared, and hashed by identity.

Then, if they want more

The cost is that each object needs a header, typically 12 bytes on a 64-bit JVM, and arrays and fields hold references rather than data. A million two-int points take about 28 megabytes instead of 8, and iterating them means chasing pointers, which is bad for cache locality, so performance code ends up with parallel primitive arrays.

Valhalla's value classes, JEP 401, which is still in preview, give up identity: == compares field values and synchronizing on one is an error, so the JVM is free to flatten them into arrays and fields.

To prepare a large codebase I treat the value-based classes, the wrappers, Optional and the java.time types, as having no identity already: no == on them, no locking on them, no identityHashCode or IdentityHashMap keys, and no deprecated wrapper constructors. I turn on javac's synchronization and removal lint as errors and add a static analysis rule.

And I keep our own small domain types immutable and final with value equality, mostly as records, so they can become value records later with just a recompile.

About this answer

Last updated
12 November 2026
Versions it names
Java 16, 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 "Project Valhalla proposes value classes whose instances have no identity. Explain why...": https://scrivio-interview-library.netlify.app/java/object-contracts/7f5f7c6898/