Write Once, Compile Many Times in Java
Definition:
Java is a platform-independent language because of its unique compilation process. The phrase
“Write Once, Compile Many Times” means that a Java program needs to be written and compiled
only once, but it can be executed (run) on any platform that has a Java Virtual Machine (JVM).
Explanation:
• When a Java program is written, it is saved with a .java extension.
• The Java compiler (javac) compiles the source code into an intermediate code called bytecode
(.class file).
• This bytecode is not specific to any operating system or hardware.
• The JVM on each platform converts this bytecode into machine code understood by that
system.
• Hence, the same bytecode can run on different platforms (Windows, Linux, macOS, etc.)
without recompilation.
Diagram:
Source Code (.java) ■ ■ Compiled using javac ▼ Bytecode (.class) ■
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ ▼ ▼ ▼ ▼
JVM (Windows) JVM (Linux) JVM (macOS) JVM (Android) ■ ■ ■ ■ ▼ ▼ ▼ ▼ Machine Code
Machine Code Machine Code Machine Code
Example:
class Example {
public static void main(String[] args) {
[Link]("Write Once, Compile Many Times");
}
}
Steps:
1 Write the program → [Link]
2 Compile → javac [Link] → Produces [Link]
3 Run on different platforms (Windows, Linux, macOS) → java Example
Conclusion:
Java programs are compiled once into bytecode. The same bytecode can be executed many times
on different platforms using JVMs. This feature ensures portability and reusability, which makes
Java a platform-independent and flexible language.
Hence, Java follows the principle: “Write Once, Compile Many Times.”