Java is unique because it is both compiled and interpreted. This process allows Java to follow the "Write Once, Run Anywhere" (WORA) philosophy.
To understand how Java works, you must understand the nested relationship between these three core components:
- JVM (Java Virtual Machine): The "engine" that actually runs the Java bytecode. It is platform-dependent (different for Windows, Mac, and Linux).
- JRE (Java Runtime Environment): Contains the JVM + Library files (jars) and other files that Java needs to run. It’s what you need to run Java apps.
- JDK (Java Development Kit): Contains the JRE + Development tools (like
javacandjdb). It’s what you need to write and compile Java apps.
The journey of a Java program happens in four distinct stages:
The programmer writes source code in a text editor or IDE (like IntelliJ or VS Code).
- File Extension:
Main.java
The developer runs the Java Compiler (javac). Instead of turning the code into machine-specific instructions (like C++ does), it turns it into Bytecode.
- Command:
javac Main.java - Result:
Main.class(Platform-independent bytecode)
When you run the program, the JVM’s ClassLoader puts the .class file into memory. The Bytecode Verifier checks the code for security violations and illegal code to ensure it won't crash the host system.
The JVM interprets the bytecode. However, to make it faster, it uses a JIT (Just-In-Time) Compiler.
- The JIT compiler analyzes the code as it runs and compiles "hot spots" (frequently used code) directly into native Machine Code for the specific processor you are using.
- Source Code (
.java) - ➜ Compiled by JDK (javac) 3. ➜ Bytecode (
.class) - ➜ Executed by JRE (JVM) 5. ➜ Interpreted/JIT Compiled to Native Machine Code (0101...)
Because of the JVM, the same .class file you compile on a Windows machine can be moved to a Linux server or a Mac laptop and it will run exactly the same way, as long as that machine has a JRE installed.