Chapter 2: How Java Works
This chapter is only about the machine story: what happens from the moment you save a .java file to the moment your program runs.
1. Topic title
How Java works (JVM, JRE, JDK, compilation, bytecode, WORA)
2. What it means
When you write Java, you are writing source code (human-readable). The Java compiler (javac) translates that into bytecode (a compact instruction format the JVM understands).
At run time, the JVM loads your classes, verifies bytecode, executes it, and talks to the OS for things like files and network sockets.
So there are two big phases:
- Compile time —
javacchecks types and syntax, emits.classfiles (bytecode). - Run time —
javastarts the JVM, loads yourmainclass, and runs bytecode.
3. Why it is used
This split is useful in the real world:
- The same
.classfiles can run on any OS that has a compatible JVM (within the same major Java world you target). - The JVM can optimize hot code while the program runs (JIT compilation).
- Teams can ship libraries as jars without giving away source code.
4. Real-world example
You build a payment microservice on your laptop. CI builds a jar or fat jar. Production runs the same bytecode on Linux servers with a matching Java version. You are not recompiling per server brand—you are relying on the JVM port for that platform.
5. Java code example
Assume a file Hello.java:
public class Hello {
public static void main(String[] args) {
System.out.println("Hello from bytecode");
}
}Typical terminal flow:
javac Hello.java
java HelloExpected output:
Hello from bytecodeAfter javac, you should see Hello.class next to your source file.
6. Explanation of code
javac Hello.javaruns the compiler on your source file.java Hellostarts the JVM and tells it to run themainmethod of classHello. Note: you pass the class name, notHello.class.
The JVM finds bytecode on the classpath (default: current directory). Later tools hide these steps, but the mental model stays the same.
7. Common mistakes
Saying “Java is interpreted only.” Bytecode starts interpreted, but the JVM JIT-compiles hot paths to native code. For learners, “compiled to bytecode, run by JVM” is enough truth.
Mixing up javac and java. Compile with javac, run with java.
Running java Hello.class. Use java Hello instead.
8. Best practices
Install a JDK, not “just Java” from a random tutorial link. You need javac for development.
Keep your JDK version in sync with teammates and CI. Small version drift causes annoying “works on my machine” bugs.
Learn where bytecode lives (target/ in Maven, build/ in Gradle). When something breaks in builds, you will search those folders.
9. Small practice task
After you install a JDK (next chapter), compile and run Hello.java above. Delete Hello.class and confirm java Hello fails until you compile again.
Beginner tip
JDK vs JRE vs JVM — quick map:
- JVM — runs bytecode.
- JRE (older concept) — JVM + enough libraries to run programs.
- JDK — JRE pieces for running plus developer tools like
javac,javadoc,jdb.
Modern installs are usually a JDK that bundles what you need to develop.