Card 01/ 06
How Java Runs: Source Code, Bytecode and the JVM
Why double-clicking Hello.java does nothing, and the two translations it needs first.
A Java program is translated twice before it runs. The compiler turns your source text into bytecode, a compact instruction set that no real processor understands, and the Java Virtual Machine (JVM), a program running on your computer, turns that bytecode into instructions your processor does understand, while the program runs.
You save Hello.java, double-click it, and nothing happens. The file is text. Your operating system does not know what to do with it, and your processor knows even less.
Every language that is not machine code has to be translated before anything can run it. Many languages translate once, straight into instructions for the processor in front of them. Java translates twice, and the second translation is what lets one build run on any machine.
public class Hello {
public static void main(String[] args) {
System.out.println("Hello");
}
}The first translation turns that text into bytecode and writes it to a file. The second happens while your program is running: the JVM reads the bytecode and issues instructions the processor underneath it can execute.
The two translations also split the work of finding mistakes. The compiler checks everything it can see in the text before any program exists; the JVM can only report what goes wrong while the program runs.
So a Java mistake reaches you at one of two completely different moments. A misspelled keyword is caught by the first translation and there is no program at all. A missing file is discovered by the second, minutes into a run, with output already on the screen.
← → move through sections and cards