Short answer
You compile your source once into platform-neutral bytecode (.class files), and that same bytecode runs on any machine that has a JVM, without recompiling.
Explanation
Java achieves it by inserting the JVM as a layer between your program and the OS: javac targets the abstract JVM instruction set instead of a specific CPU, and each OS ships its own JVM that translates that bytecode into native instructions at run time. The bytecode is the same everywhere; only the JVM underneath differs.
Common mistakes
- Claiming Java is interpreted line by line — modern JVMs JIT-compile hot code to machine code.
- Saying 'run anywhere' means no testing on other platforms is ever needed.