The JVM and Your First Program
Compile Java to bytecode, run it on the JVM, and write a minimal Hello World with a correct main method.
Why it matters
Start from the concept, not the syntax
Meet a concrete situation where this idea earns its place, then state the boundary it guards.
Every Java program takes the same road: source code becomes bytecode, and bytecode runs on the Java Virtual Machine. Understanding that road explains why one compiled program runs unchanged on Windows, macOS, and Linux—and why the toolkit you install matters.
By the end of this chapter
A first program fails with a mismatched file name, a missing main method, or a forgotten compile step—and the errors do not explain which of the three it was.
- JDK, JRE, and JVM roles
- Source vs bytecode
- The main method contract
Mechanism
How Java expresses the concept
Walk the actual types, rules, and runtime behavior involved—the same behavior described in the Java Language Specification.
The mental model
Java source lives in `.java` files. The `javac` compiler turns source into platform-neutral bytecode stored in `.class` files. At runtime the Java Virtual Machine (JVM) loads that bytecode and executes it, translating bytecode to native machine code with its just-in-time (JIT) compiler.
This split is why Java is described as "write once, run anywhere." Your program never talks to the operating system directly; it talks to the JVM, and the JVM talks to the OS. The JDK (Java Development Kit) gives you `javac`, `java`, and the standard library. The JRE (Java Runtime Environment) is what end users need to run programs; on modern Java the JRE is a generated runtime image rather than a separate download.
A Java program is organized into classes. Execution starts in a class that declares a `public static void main(String[] args)` method. `public` makes it reachable, `static` means it runs on the class itself without an object, `void` means it returns nothing, and the `String[] args` receives command-line arguments.
Concept map
Inspect the building blocks as a whole
Use the map as a structural reference; it shows the pieces and their relationships, not an execution sequence.
Core Java
The JVM, the JDK, and your first program
Java compiles to bytecode that runs on the Java Virtual Machine, so the same class file runs on any platform with a compatible JVM.
- 01Source (.java) vs bytecode (.class)
- 02JDK, JRE, and JVM roles
- 03Write once, run anywhere
- 04The main method signature
- 05Compile then run
- 06The classpath and module path
Implementation
Encode the smallest runnable example
Write and run the code, then observe the behavior you just predicted with a real Java program.
Runnable snippet path
Predict, run, and change each example
Compile and run a Hello World
Save this as Hello.java, compile with `javac Hello.java`, then run with `java Hello`.
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, Java");
}
}Remember
The file name must match the public class name, including capitalization.
Expected output
Hello, Java
Read a command-line argument
Arguments arrive as a String array; index 0 is the first value passed.
public class Greet {
public static void main(String[] args) {
if (args.length > 0) {
System.out.println("Hi " + args[0]);
}
}
}Remember
Always check args.length before reading an index, or you will hit ArrayIndexOutOfBoundsException.
Change one input, predict the result, then compile or test it before continuing.
Watch out
Break it on purpose, then repair the right layer
Predict the symptom before running the failure case, and close with the completion checklist.
Deliberate failure
Break
Save the class as hello.java but declare the public class as Hello, then try to run it without compiling first
Observe
The launcher reports it cannot find or load the main class
Repair
Match the file name to the public class exactly and compile before running, so the JVM has a .class file to load
Why it matters
Match the file name to the public class exactly and compile before running, so the JVM has a .class file to load
Completion checklist
- Explain the JDK, JRE, and JVM split
- Compile source to bytecode and run it
- Declare a correct public static void main method
Go deeper
Official references
The primary sources behind this chapter. Every claim above stays consistent with them.