Java Developer Interview Question

What does volatile actually guarantee, and when is it not enough?

What the interviewer is probing, how to structure your answer, and a spoken example you can adapt.

Quick answer

volatile guarantees visibility and ordering: a write is seen by any thread that subsequently reads that field, and it establishes a happens before edge so writes made before it are visible too. It does not give atomicity, so count plus plus is still a race because it is a read, an add and a write. For compound actions use synchronized, a lock, or an atomic class such as AtomicLong.

Why interviewers ask this

It is the cleanest way to find out whether someone understands the Java memory model or just knows that volatile means shared. The interviewer wants the visibility versus atomicity distinction stated precisely, plus a valid use case such as a stop flag. Follow ups tend to go to double checked locking, the atomic classes and how synchronized provides both mutual exclusion and the same memory effects.

How to structure your answer

  • Split the guarantee into visibility and ordering.
  • State clearly what it does not do: compound operations stay racy.
  • Give a legitimate use, such as a flag that stops a loop.
  • Name what you use instead when you need atomicity.

Example answer

Spoken example, first person

volatile means a read always sees the most recent write rather than a value cached in a register or a core's cache, and it stops the compiler and processor reordering around it. The memory model way to say that is it creates a happens before edge, so everything the writing thread did before the volatile write is visible to a thread that reads it afterwards. What it does not give me is atomicity. A counter increment is three operations, so two threads can both read the same value and one update is lost no matter how volatile the field is. My usual valid use is a boolean flag that tells a background loop to stop, where a single write and a single read is exactly the pattern. Anything read modify write goes to an AtomicInteger, a LongAdder if it is hot enough that contention matters, or a lock if several fields have to change together. The other place I rely on the memory model is safe publication, since final fields are guaranteed visible after construction, which is a good reason to make things immutable.

Walking into this interview soon? GhostPilot listens to your live call, spots the question the moment it is asked, and puts a structured answer on your screen in real time. Try it on your next mock, or grab a $29 Session Pass, no subscription, for the real thing.

See how it works

Follow-up questions to expect

  • Why was double checked locking broken before volatile was fixed?
  • How does synchronized give you the same memory guarantees?
  • When would you use LongAdder rather than AtomicLong?

Related java developer questions

Your interviewer will ask their own version of this. Paste your actual job description into the free Question Predictor and get the 20 questions that role is most likely to ask, with what each one is really probing.

Predict my questions

Rehearse the hard questions before they are asked

Practise with a live copilot, then walk in ready. A $29 Session Pass gets you through the interview with no subscription and no lock-in.

Get GhostPilot