Module 1: The JVM & Execution (The Engine)
📚 Module 1: The JVM & Execution
Focus: Moving from “Code” to “Universal Execution.”
Java’s famous motto is: “Write Once, Run Anywhere.” To understand how this works, we must look at the engine under the hood: the Java Virtual Machine (JVM).
🏗️ Step 1: Bytecode (The “Universal Translator”)
In many languages, you compile code for a specific computer (Windows, Mac, or Linux). Java does something different.
- The Analogy: Imagine you write a book in English. Instead of translating it into 100 languages, you translate it into Esperanto (a universal language).
- Anyone with an “Esperanto Reader” (The JVM) can read your book, no matter where they are from.
In Java:
- Code: You write
.javafiles. - Compile: The Java Compiler (
javac) turns them into.classfiles containing Bytecode. - Run: The JVM reads the bytecode and tells the computer what to do.
🏗️ Step 2: The JVM Memory (The “Hotel”)
The JVM manages memory for you so you don’t have to manually delete every object.
🧩 The Analogy: The Hotel Guest List
- The Stack: Temporary memory for things happening right now (like a local variable in a function). It’s like a notepad on the receptionist’s desk—clean it off as soon as the call ends.
- The Heap: Long-term memory where your “Guests” (Objects) live.
- Garbage Collector (GC): The hotel maid. When a guest leaves the hotel (no more references to an object), the maid cleans the room instantly to make it ready for the next guest.
🏗️ Step 3: Strong Typing (The “Container Port”)
Java is Statically Typed. You must tell the computer exactly what kind of “container” you are using before you put something in it.
🧩 The Analogy: The Cargo Ship
- If you have a box for “Bananas,” you cannot put “Cars” in it.
- In Code:
int apples = 5; // Valid
apples = "five"; // ERROR! The box only holds numbers.Why is this good? Because it catches bugs before you even run the program. The computer warns you: “Hey, you’re trying to put a car in a banana box!”
🧪 Step 4: Your First Java Program
public class HelloWorld {
// This is the starting point of EVERY Java app
public static void main(String[] args) {
String greeting = "Hello, Java World!";
System.out.println(greeting);
}
}Breaking it down:
public class HelloWorld: Every bit of code in Java must live inside a “Class.”public static void main: The “Power Button” of the app.System.out.println: How you print something to the screen.
🥅 Module 1 Review
- JVM: The virtual computer that runs your code.
- Bytecode: The universal language of Java.
- Heap & Stack: How Java organizes its memory.
- Garbage Collector: The automatic cleanup crew.
- Static Typing: Telling the computer the data type upfront.