Careeroza
How Java Code Runs
From Source Code to Output
Main.java --javac--> Main.class --java--> JVM ---> Output
(source) (compiler) (bytecode) (launcher) |
+-- Class Loader (loads .class files)
+-- Bytecode Verifier (safety checks)
+-- Interpreter + JIT (runs, compiles hot code)
+-- Garbage Collector (frees memory)Step by Step
- Write source code in a
.javafile. - Compile:
javacchecks syntax and types and produces bytecode in a.classfile. Bytecode is not machine code for any real CPU; it is code for the JVM. - Load: the JVM class loader reads the
.classfile. - Verify: the bytecode verifier checks that the code is safe.
- Execute: the interpreter runs the bytecode. Code that runs often ("hot" code) is compiled to fast native machine code by the JIT (Just-In-Time) compiler.
Compile-time vs Runtime Errors
Some mistakes are caught by javac before the program runs (compile-time errors), others only appear while the program runs (runtime errors / exceptions).
int age = "twenty"; // compile-time error: incompatible types: String cannot be converted to intExample
This program compiles fine, but dividing an integer by zero fails at runtime:
public class Main {
public static void main(String[] args) {
int total = 100;
int people = 0;
System.out.println("Splitting the bill...");
System.out.println(total / people);
}
}Output
Splitting the bill... Exception: java.lang.ArithmeticException: / by zero
Tip: Compile-time errors are your friend: Java finds many bugs before the code ever reaches users.