Quick revision
The short answer
It shows that a specification deliberately promises less than most implementations deliver, so that the language can run efficiently on a wide range of hardware and JVMs, now and in the future. Code that depends on the stronger behaviour of today's 64-bit JVMs is not portable and can break silently on another platform, JVM or JIT version. So we write against the specification: volatile, atomics or locks wherever atomicity or visibility matters, and we treat strong guarantees as something that is chosen deliberately because each one has a cost.
What to remember
In this answer, we saw that the long and double rule survives from 32-bit hardware because a specification must describe every conforming implementation and deliberately keeps its guarantees weak to leave room for efficient JVMs and processors. Implementations usually deliver more, and code that depends on that extra behaviour is not portable and can break on a new platform, JVM or JIT. So we write against the specification, using volatile, atomics, locks and immutable snapshots where atomicity or visibility matters. Valhalla's value classes reopen the tearing question for larger values, and in every case the lesson is that strong guarantees cost performance and are chosen deliberately.
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.
It shows that a specification deliberately promises less than most implementations deliver, so that the language can run efficiently on a wide range of hardware and JVMs, now and in the future. Code that depends on the stronger behaviour of today's 64-bit JVMs is not portable and can break silently on another platform, JVM or JIT version. So we write against the specification: volatile, atomics or locks wherever atomicity or visibility matters, and we treat strong guarantees as something that is chosen deliberately because each one has a cost.
Background to this question
At a senior level, this question is less about long and double than about engineering judgement: how do we decide which behaviour our code may rely on? The same tension appears with memory ordering, hash iteration order, thread scheduling and many library details. In this answer, we will look at where the long and double rule comes from, why specifications keep weak guarantees even when most hardware is stronger, how depending on implementation behaviour goes wrong, how to write code against the specification, what Project Valhalla brings to the tearing discussion, and why strong guarantees are never free.
1Where the Rule Comes From
JLS section 17.7 allows a write to a non-volatile long or double to be performed as two 32-bit writes, so a racing reader can see one half of a new value and one half of an old one. When Java was designed, many target processors were 32-bit, and an atomic 64-bit store either did not exist or needed expensive extra instructions. Requiring atomicity for every long field would have made ordinary arithmetic code slower on that hardware.
Today almost every server is 64-bit, and HotSpot writes longs with a single instruction. Yet the rule is still in the specification. Java still runs on 32-bit ARM devices and embedded JVMs, and the specification has to describe every conforming implementation, not only the most common one. Removing the permission would also gain little, because code that needs atomicity already has volatile.
2Why Specifications Promise Less Than Implementations Deliver
A specification is a contract between two sides. Programmers rely on what it promises, and implementers are free to do anything it does not forbid. Every guarantee added to the contract takes freedom away from every future JVM, compiler and processor. So designers keep guarantees as weak as they can while still letting programmers write correct code, and they give programmers an explicit way to ask for more when they need it.
The Java Memory Model is the clearest example. It allows a great deal of reordering of plain reads and writes, which lets the JIT keep values in registers, move loads out of loops and merge stores, and lets the processor use store buffers and caches freely. In exchange, programmers get volatile, synchronized, final fields and the java.util.concurrent classes, which create happens-before edges exactly where they are needed. The long and double rule is a small, old instance of the same bargain.
3How Relying on the Implementation Goes Wrong
Implementations often give stronger behaviour than the specification requires, and that is where the trap is. x86 processors keep stores in order across cores far more strictly than the JMM demands, HotSpot on 64-bit writes longs atomically, and HashMap iteration order happens to be stable for a given set of keys and JDK version. Code that quietly depends on any of these works, passes tests and ships.
Then something changes. The fleet moves to ARM servers, which allow more reordering. A JDK upgrade brings a new JIT optimisation that hoists a plain read out of a loop. A library changes its hashing. The code was never correct, but now it fails, and it fails rarely and far from the change that exposed it, which makes it very expensive to diagnose.
| Area | The specification promises | Typical behaviour today |
|---|---|---|
| plain long and double writes | may be split into 32-bit halves | one 64-bit store on 64-bit HotSpot |
| ordering of plain writes | almost none without happens-before | x86 keeps stores in order; ARM does not |
| visibility of a plain field | none without synchronization | usually seen soon, unless the JIT hoists it |
HashMap iteration order | unspecified | stable for one JDK and key set |
4Writing Code Against the Specification
The practical rule is: if a value is shared between threads and its atomicity or visibility matters, say so explicitly, using a construct whose guarantee is in the specification. For a single 64-bit value that means a volatile field or an atomic class. For several values that must change together it means a lock or an immutable object published through a volatile reference. For finer control, VarHandle offers documented access modes such as opaque, acquire and release, which are for experts and should be rare.
Notice that nothing here is exotic. Writing against the specification is mostly about declaring intent with ordinary keywords, and reviewers can check it without knowing which hardware the code will run on.
5Valhalla Brings the Question Back for Larger Values
The long and double rule may look like history, but Project Valhalla brings the same question back. Valhalla proposes value classes, objects without identity whose fields can be stored flat inside arrays and other objects. A flattened value with several fields may be larger than any single atomic machine write. If a racing thread writes such a value while another reads it, the reader could see fields from two different writes, which is tearing again, at a larger scale.
The proposals discuss how to handle this: keep flattened values atomic by default, which limits how much can be flattened, or let a class explicitly accept that it may tear in exchange for better layout. The details have changed between drafts, so check the current JEP text for your Java version. The point for this answer is that the language designers are making the same deliberate trade-off again: atomicity is a guarantee with a cost, and they want programmers to choose it consciously.
6Strong Guarantees Cost Performance
Every guarantee is paid for somewhere. Making every long write atomic on 32-bit hardware would have cost extra instructions on every store. Making every field access sequentially consistent, as if all threads shared one global order, would forbid most JIT and processor optimisations and would need memory barriers on almost every access, which could slow ordinary code several times over. That is why volatile is opt-in rather than the default.
So the specification chooses its guarantees deliberately: strong where the cost is small and the benefit is large, such as atomic references, which keep Java memory-safe even with races, and weaker where most code does not need the guarantee, such as ordering of plain fields. Our job is to ask for the stronger guarantee in exactly the places that need it, no more and no less.
7Follow-up questions
If the risk is so small on 64-bit JVMs, is it worth making every shared long volatile?
Yes, when the field is genuinely shared without a lock. The cost of volatile on a single value is tiny, and it also fixes visibility, which is the more common real failure. More importantly, it documents that the field is shared, which helps the next reader of the code.
How do you keep a large team from relying on implementation behaviour?
By making the specification the review standard: shared mutable fields must be volatile, atomic, guarded by a documented lock or immutable, and static analysis checks enforce the common cases in the build. Code that relies on stronger behaviour must say so in a comment and have an owner, and I revisit it on JDK or hardware changes.
Are there other places where Java specifies less than implementations do?
Many. HashMap and HashSet iteration order, the order in which threads are scheduled or woken, the exact timing of garbage collection and finalisation, and the identity hash code values. Each of them behaves predictably on one JVM version, and each has broken code that depended on that predictability.
8Conclusion
In this answer, we saw that the long and double rule survives from 32-bit hardware because a specification must describe every conforming implementation and deliberately keeps its guarantees weak to leave room for efficient JVMs and processors. Implementations usually deliver more, and code that depends on that extra behaviour is not portable and can break on a new platform, JVM or JIT. So we write against the specification, using volatile, atomics, locks and immutable snapshots where atomicity or visibility matters. Valhalla's value classes reopen the tearing question for larger values, and in every case the lesson is that strong guarantees cost performance and are chosen deliberately.
Open with
For me this rule is a good example of how specifications work. The JLS still allows a plain long or double write to be split into two 32-bit halves because Java has to run on every conforming JVM and processor, including 32-bit and embedded ones, and every guarantee in the specification removes freedom from every future implementation.
Then, if they want more
So the specification promises as little as it can and gives us explicit tools, volatile, locks and atomics, to ask for more. Implementations often deliver more anyway: 64-bit HotSpot writes longs atomically and x86 keeps stores in order.
The danger is code that depends on that, because it works and passes tests until we move to ARM, upgrade the JDK or change JVMs, and then it fails rarely and far from the cause.
So my rule is to write against the specification: if a value is shared and atomicity or visibility matters, make it volatile or atomic, or guard it with a lock, and reviews check exactly that.
I would also mention Valhalla, where value classes bring back the question of tearing for larger flattened values; the proposals discuss making that an explicit choice, so I would check the current JEP. The general point is that strong guarantees cost performance, so specifications choose them deliberately, and so should we.
About this answer
- Last updated
- 12 November 2026
- 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
- JLS chapter 17, Threads and Locks
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 "The JLS still allows non-volatile long and double writes to be non-atomic, a rule that...": https://scrivio-interview-library.netlify.app/java/java-memory-model/2e27fb080d/