Quick revision
The short answer
A value-based class is a final, immutable class whose instances are meant to be treated purely as values: equals, hashCode and toString depend only on their state, and the class makes no promise about object identity. The JDK may return the same instance or different instances for equal values, so == on two Optionals can give results that differ between JDK versions, and synchronizing on one is discouraged because future JDKs may turn such classes into value classes that have no identity and no monitor. We should compare Optionals with equals or by checking their contents.
What to remember
In this answer, we saw that a value-based class is final and immutable, defines equals, hashCode and toString only by its state, and promises nothing about identity, so equal instances must be interchangeable. Because of that, == on Optionals can give different results between JDK versions, as the shared empty instance and the Integer cache show, and synchronizing on them is both a bad lock today and a failure once such classes become identity-free value classes. Comparing with equals, or checking the contents with isPresent, isEmpty, map or filter, is always correct.
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.
A value-based class is a final, immutable class whose instances are meant to be treated purely as values: equals, hashCode and toString depend only on their state, and the class makes no promise about object identity. The JDK may return the same instance or different instances for equal values, so == on two Optionals can give results that differ between JDK versions, and synchronizing on one is discouraged because future JDKs may turn such classes into value classes that have no identity and no monitor. We should compare Optionals with equals or by checking their contents.
Why this question is asked
Most of the time we never think about whether two Optionals are the same object. The question matters when code accidentally depends on it: an == comparison that passes in tests and fails after a JDK upgrade, or a synchronized block on an Optional that later produces a warning or an error. Interviewers ask about value-based classes because the term appears in the Javadoc of Optional, LocalDate, Integer and many others, and understanding it shows we know where Java is heading with Project Valhalla. In this answer, we will recap identity versus equality, define value-based classes, reproduce the == problem, see why synchronizing is wrong, and finish with the correct ways to compare.
1A Quick Recap of Identity and Equality
Every ordinary Java object has an identity: it is a particular object at a particular place in memory. The == operator on references compares identity, asking "are these the same object?". The equals method compares state, asking "do these objects represent the same value?", as long as the class overrides it.
Identity also brings other features: every object has a monitor that synchronized can lock, and System.identityHashCode gives a number tied to the object rather than its contents. For many classes these features matter. For classes that just represent a value, like a date or an optional result, they are accidental and can even get in the way.
2What the JDK Means by Value-Based
The JDK documentation lists the properties of a value-based class. It is final and immutable, although it may hold references to mutable objects. Its equals, hashCode and toString are computed only from its state, not from identity. It has no accessible constructors; instances come from factory methods such as Optional.of or LocalDate.of, and those factories make no promise about whether they return a new object or an existing one. And two equal instances are freely interchangeable: any code that behaves differently depending on which of two equal instances it received is wrong.
That last point is the heart of it. Because identity is not meaningful, the JDK is free to cache instances, to create new ones, or in the future to drop identity entirely. Optional, the primitive wrappers such as Integer, the java.time classes and the immutable collections from List.of are all documented as value-based.
| Operation | Safe? | Why |
|---|---|---|
a.equals(b) | Yes | Depends only on state |
a.hashCode(), toString() | Yes | Depends only on state |
| a == b | No | Identity is not specified and may change |
| synchronized (a) | No | Future value classes may have no monitor |
System.identityHashCode(a) | No | Identity is not meaningful |
As a key in IdentityHashMap | No | Relies on identity |
3Reproducing the == Problem
Here is what == does today on Optionals and, as a familiar comparison, on Integer, which is also value-based.
Notice that the results depend on implementation details: Optional.empty() happens to return a single shared instance, and Integer caches values from -128 to 127 by default. None of this is promised. A JDK could cache common Optionals, or stop sharing the empty one, or, once value classes arrive, compare such objects by their state when we use ==. Code that relies on today's answer can change behaviour after an upgrade without a single line being modified, which is why the result is described as unspecified.
4Why Synchronizing on an Optional Is Wrong
Synchronizing on a value-based object has two problems. Today, it is simply a bad lock: because the factory may hand the same instance to unrelated code, two parts of the program that each lock "their" Optional.empty() may actually share one lock and block each other for no reason, and code that locks a fresh Optional.of(x) locks an object nobody else can see, which protects nothing.
The bigger reason is the future. Project Valhalla is working on value classes, whose instances have no identity at all. A value object has no monitor, so synchronizing on it cannot work and would throw at runtime. The JDK is preparing for that: since Java 16, javac reports a warning when we synchronize on an instance of a class it knows is value-based (under the synchronization lint category), and HotSpot has a diagnostic option, -XX:DiagnoseSyncOnValueBasedClasses, that can log or fail such synchronization at runtime for JDK classes marked internally as value-based. Check your JDK version for the exact flags.
So the rule is to lock only objects whose identity we control, typically a private final Object or a ReentrantLock, and never a value.
5Comparing Optionals Correctly
Since equality is defined by state, Optional.equals does the right thing: two Optionals are equal if both are empty, or if both hold values that are equal by their own equals method. For most code we do not even need to compare Optionals; we compare what is inside them.
The last line deserves attention, because comparing with Optional.empty() using == is a real bug that works today only because of the shared instance. isEmpty and isPresent state the intent without depending on identity.
6Follow-up questions
Does the compiler stop me from using == on Optionals?
No. javac does not warn about == on value-based classes, although static analysis tools such as Error Prone and IDE inspections can flag reference comparison of Optionals. The compiler warning added in Java 16 covers synchronization, not equality.
What is the relationship between value-based classes and value classes?
Value-based is today's contract: classes that promise not to rely on identity, so that the JDK keeps the freedom to change them. Value classes are a planned language feature from Project Valhalla, still in preview work, whose instances really have no identity and can be laid out flat in memory. The value-based contract exists so that classes like Optional and Integer can become value classes later without breaking correct code.
Can I use an Optional as a key in a HashMap?
It works, because HashMap uses equals and hashCode, which Optional defines from its contents. It is usually a design smell, though, since Optional is meant as a return type. An IdentityHashMap, on the other hand, must never be used with value-based keys, because it compares by identity.
7Conclusion
In this answer, we saw that a value-based class is final and immutable, defines equals, hashCode and toString only by its state, and promises nothing about identity, so equal instances must be interchangeable. Because of that, == on Optionals can give different results between JDK versions, as the shared empty instance and the Integer cache show, and synchronizing on them is both a bad lock today and a failure once such classes become identity-free value classes. Comparing with equals, or checking the contents with isPresent, isEmpty, map or filter, is always correct.
Open with
Value-based is a contract the JDK documents for classes like Optional, Integer, LocalDate and the List.of collections. Such a class is final and immutable, its equals, hashCode and toString depend only on its state, it is created through factory methods, and it makes no promise about identity: the factory may give me a new object or an existing one, and two equal instances should be completely interchangeable.
Then, if they want more
That has two consequences. First, double equals on two Optionals is unreliable. Optional.empty happens to return one shared instance today, and Integer caches small values, so equality by reference sometimes works and sometimes doesn't, and the answer could change in a later JDK.
So I always compare with equals, or better, I compare the contents with isPresent, isEmpty, map or filter. Second, I never synchronize on such an object.
Today it is a poor lock, because unrelated code might share the same cached instance, and in the future Project Valhalla plans value classes that have no identity and therefore no monitor at all, so synchronizing would fail. Since Java 16 javac already warns about synchronizing on value-based classes, and HotSpot has a diagnostic flag for it at runtime.
The right lock is a private final object or a ReentrantLock that I own.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 16, Java 11
- 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
- java.util.stream, package documentation, java.util.function, package 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 "Optional is a value-based class. What does that mean, and why should code not use == or...": https://scrivio-interview-library.netlify.app/java/optional/1fdbbb827b/