Quick revision
The short answer
Integer, Long and the other wrappers are documented as value-based classes, which means code must not rely on their identity: no ==, no synchronized blocks on them, no identityHashCode. JDK 16 deprecated the wrapper constructors for removal and added diagnostics for synchronizing on such objects, and Project Valhalla plans to turn them into value classes without identity, where locking on them could fail. I would plan for it by taking an inventory with static analysis and the JVM diagnostic, fixing the code with dedicated lock objects and equals, and adding lint rules so new code cannot bring the pattern back.
What to remember
In this answer, we saw that wrappers are value-based classes, so == and synchronization on them depend on caching and are already bugs. JDK 16 deprecated their constructors and added compile-time and runtime diagnostics, and Valhalla plans to remove their identity, which would make locking on them fail. The plan is to inventory with static analysis and -XX:DiagnoseSyncOnValueBasedClasses in test runs, fix with dedicated locks and equals, enforce through shared lint rules, and prioritise by present risk.
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.
Integer, Long and the other wrappers are documented as value-based classes, which means code must not rely on their identity: no ==, no synchronized blocks on them, no identityHashCode. JDK 16 deprecated the wrapper constructors for removal and added diagnostics for synchronizing on such objects, and Project Valhalla plans to turn them into value classes without identity, where locking on them could fail. I would plan for it by taking an inventory with static analysis and the JVM diagnostic, fixing the code with dedicated lock objects and equals, and adding lint rules so new code cannot bring the pattern back.
Background to this question
Leadership questions about upgrades test whether we can turn a technical warning into a plan that many teams can follow without stopping their own work. This one is a good example: the warning looks minor today, but it points at a future JDK change that could make some of our code fail at runtime. In this answer, we will recap what value-based classes are and why locking on a wrapper is already a bug, look at what the JDK has done so far and what is planned, and then walk through a plan to find, fix and prevent the pattern across many teams, including how to prioritise it.
1A Quick Recap of Value-Based Classes
A value-based class is one whose instances should be treated only as values. The JDK documentation lists them: the primitive wrappers such as Integer and Long, Optional, and the java.time types such as LocalDate. For these classes, two instances with equal contents are meant to be interchangeable. The JDK may cache them, reuse them, or create new ones at any time, and it promises nothing about which object we get.
Because of that, any operation that depends on which particular object we hold is unreliable. That includes == between instances, synchronizing on an instance, and System.identityHashCode. They compile and run, but their results depend on caching decisions we do not control.
2Why Synchronizing on a Wrapper Is Already a Bug
Teams sometimes lock on an id to serialise work for one customer or one account. It looks reasonable, but the lock object is not what they think it is.
For small ids, every caller in the whole JVM shares the same cached Long, so unrelated code can block on it. For large ids, each caller boxes its own Long, so two threads crediting account 5000 lock different objects and run at the same time. The code is wrong in both directions, before any future JDK change.
3What the JDK Has Done and What Is Coming
JDK 16 (through JEP 390) took the first steps. It deprecated the wrapper constructors such as new Integer(5) for removal, because a constructor always creates a fresh identity, which conflicts with treating these classes as values. It added a javac lint warning for synchronizing on an instance of a value-based class, and a HotSpot option, -XX:DiagnoseSyncOnValueBasedClasses, that detects such synchronization at runtime and either logs it or makes it a fatal error, depending on the value we pass (check your JDK documentation for the exact values). JFR also records an event for it.
The reason is Project Valhalla. Its value classes have no identity, so there is nothing to lock. The plan is for the wrappers and other value-based classes to become value classes, and synchronizing on a value object is expected to throw an exception. Code that locks on a Long would then go from subtly wrong to failing outright.
- Today== and locking on wrappers compile, run, and depend on caching
- JDK 16 and laterconstructors deprecated for removal; javac and runtime diagnostics
- Valhalla value classeswrappers lose identity; locking on them is expected to fail
3 steps in order. Play them, or step through them one at a time.
The diagnostics exist so that we can fix the code before it breaks.
4A Plan Across Many Teams
The first step is an inventory, and it should be automatic. We run static analysis across all repositories: javac with -Xlint:synchronization, plus checks for reference comparison of boxed values and for the deprecated constructors. In parallel, we start the integration and performance test environments with -XX:DiagnoseSyncOnValueBasedClasses in logging mode, which catches cases static analysis misses, such as locks on objects typed as Object that turn out to be Integers at runtime, including inside libraries.
The second step is fixing, and the fixes are mechanical. A lock on a wrapper becomes a lock on a dedicated object, either a private final Object lock = new Object() field or, for per-key locking, a map of lock objects. == between wrappers becomes equals or Objects.equals, and new Integer(x) becomes Integer.valueOf(x). A platform team can provide the per-key lock utility and example pull requests, so each team spends hours, not weeks.
The third step is prevention. We add these checks to the shared lint configuration that every service inherits and make them errors, not warnings. Once the existing findings are fixed, the build fails for any new occurrence, so the problem cannot grow back while teams move on. In a real system the lock map also needs a cleanup strategy, or a striped lock with a fixed number of locks, which is a detail the shared utility should solve once.
5How to Prioritise It Against Other Upgrade Work
I would split the work by risk. Locks on wrappers are correctness bugs today, because they either serialise unrelated work or fail to exclude concurrent work, so those findings go first, regardless of Valhalla. Reference comparisons are also live bugs and come next. The deprecated constructors are harmless today and can be fixed in bulk by an automated rewrite tool such as OpenRewrite. With the inventory in hand, we can give each team a number and a deadline that fits their normal upgrade cycle, rather than an open-ended request.
6Follow-up questions
How would you prioritise this against other upgrade work?
By risk rather than by the age of the warning. Synchronizing on wrappers and comparing them with == are bugs today, so I would fix them first, in the same quarter, ideally with a shared utility and example changes. Deprecated constructor calls carry no runtime risk yet, so I would batch them with an automated rewrite during a routine upgrade. The lint rule goes in immediately so the counts only go down.
Why not just wait until Valhalla ships?
Because the current behaviour is already wrong, because the fix is cheaper while the code is fresh in people's minds, and because waiting turns a quiet clean-up into an urgent blocker on a future JDK upgrade. The diagnostics exist precisely to give us that lead time.
Can a library we depend on have the same problem?
Yes, and static analysis of our own code will not see it. Running tests with the runtime diagnostic in logging mode shows synchronization on value-based instances wherever it happens, including in third-party code, which we can then report upstream or replace.
7Conclusion
In this answer, we saw that wrappers are value-based classes, so == and synchronization on them depend on caching and are already bugs. JDK 16 deprecated their constructors and added compile-time and runtime diagnostics, and Valhalla plans to remove their identity, which would make locking on them fail. The plan is to inventory with static analysis and -XX:DiagnoseSyncOnValueBasedClasses in test runs, fix with dedicated locks and equals, enforce through shared lint rules, and prioritise by present risk.
Open with
I would start by explaining to the teams that this is not only a future problem. Integer and Long are documented as value-based classes, so identity-sensitive operations on them, ==, synchronized and identityHashCode, depend on caching we do not control.
Then, if they want more
Locking on a boxed account id is broken today: small ids share a cached object across the whole JVM, and large ids give each caller its own object, so there is no exclusion.
JDK 16 deprecated the wrapper constructors for removal and added diagnostics, a javac lint warning and the DiagnoseSyncOnValueBasedClasses runtime option, because Valhalla plans to make these classes value classes without identity, where synchronizing on them is expected to fail. My plan has three steps.
First, inventory automatically: static analysis across all repositories, and the runtime diagnostic in logging mode in our test environments, which also catches library code. Second, fix with a shared approach: a dedicated lock object or a small per-key lock utility owned by the platform team, equals instead of ==, and an automated rewrite for the deprecated constructors.
Third, prevent regression by adding the rules to the shared lint configuration as build errors. For prioritisation, I would put the locking and == findings first because they are live correctness bugs, and fold the constructor clean-up into routine upgrade 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.
- 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 code relies in places on synchronizing on Integer or Long wrapper...": https://scrivio-interview-library.netlify.app/java/types-and-values/418cc0c3c5/