Welcome to the second chapter of our Core Java series. While many developers jump straight into writing syntax, elite engineers understand how the language works under the hood. In this section, we will demystify the core components of the Java ecosystem, understand how code is actually executed, and write our first program.
To write, compile, and run Java code, you need specific tools installed on your system. These tools exist in a nested hierarchy: JVM < JRE < JDK.
The JVM is the absolute core of Java's platform independence. It is a virtual computer that runs inside your actual computer.
- Role: It takes platform-independent Bytecode (
.classfiles) and translates it into platform-specific Machine Code (0s and 1s) that your CPU can understand. - Note: While Bytecode is write-once-run-anywhere, the JVM itself is platform-dependent (you need a specific JVM for Windows, another for Mac, etc.).
If you only want to run a Java application (like a game or software) but not write code, you only need the JRE.
- Formula:
JRE = JVM + Class Libraries - Class Libraries: Pre-written code that helps your program interact with the system (e.g., libraries to print text to the screen, read files, or connect to a network). The JVM needs these libraries to execute your program properly.
If you want to develop (write and compile) Java applications, you need the JDK.
- Formula:
JDK = JRE + Development Tools - Development Tools: Includes the compiler (
javac), debuggers, JavaDoc generators, and other utilities required to build software.
Summary: You write code using the JDK, your code is run within the JRE, and the actual translation to hardware instructions is done by the JVM.
To understand how the JVM works, we first need to understand the two main ways human-readable code is converted into machine code.
[Image comparing compiler vs interpreter execution flow]
| Feature | Compiler | Interpreter |
|---|---|---|
| Execution Method | Scans the entire program at once and translates it into machine code before running. | Scans and translates the program line-by-line, executing each line immediately. |
| Startup Speed | Slower (requires full translation before execution begins). | Faster (starts executing right away). |
| Overall Speed | Faster (machine code is already optimized and ready). | Slower (translation happens continuously during execution). |
| Examples | C, C++ | Python, Ruby |
Is Java a compiled language or an interpreted language? It is both.
Java designers wanted the best of both worlds: the speed of a compiler and the flexibility/platform independence of an interpreter. Here is the exact flow:
- You write source code in a
.javafile. - The JDK's compiler (
javac) reads the entire file. - Instead of converting it to machine code, it converts it to Bytecode (a
.classfile).
When you run the Bytecode, the JVM takes over. Inside the JVM, two components work together:
- The Interpreter: It starts reading the Bytecode line-by-line and translating it to machine code. This allows the Java program to start up very quickly.
- The JIT (Just-In-Time) Compiler: While the interpreter runs, the JVM acts as a spy. It watches for "hot code"—blocks of code that are executed repeatedly (like a massive
forloop). The JIT compiler takes this frequent code, compiles it all at once into highly optimized machine code, and caches it.
- Result: The next time that loop runs, the JVM skips the slow line-by-line interpreter and uses the lightning-fast cached machine code.
Translating code isn't the JVM's only job. It provides two massive benefits:
- The Sandbox Security Model: Because Java was designed to be downloaded from the internet and run locally, the JVM runs your code in a restricted "sandbox." It prevents malicious Bytecode from accessing your file system, deleting files, or planting viruses.
- Automatic Garbage Collection: In older languages like C++, programmers had to manually allocate and free up RAM. If they forgot, the computer would crash. The JVM automatically detects when objects in memory are no longer being used and deletes them to free up space.
You might hear different acronyms for Java. Here is what they mean:
- JSE (Java Standard Edition): Known as "Core Java." This is what you are learning right now. It contains all the foundational libraries, OOP principles, and data structures.
- JEE (Java Enterprise Edition / Jakarta EE): Built on top of JSE. It includes additional libraries for building massive, secure, server-side web applications and microservices (Tools like Spring Boot rely heavily on JEE concepts).
- JME (Java Micro Edition): A lightweight version created for early mobile phones and embedded systems. It is largely obsolete today, having been replaced by Android development (which uses core Java/Kotlin).
Let's put this theory into practice. Ensure you have the JDK installed on your machine and properly added to your system's PATH variables.
Create a file named Demo.java and write the following code:
public class Demo {
public static void main(String[] args) {
System.out.println("Hello World");
}
}