Day 1 of 22 · ~10 min
What Java is, and your first program
Objective. Explain how Java runs, then write, compile, and run a program.
Why this matters. Everything you build rests on one loop: you write human-readable code, and Java turns it into something that runs anywhere. Get the loop working once and every later lesson fills it in.
How Java actually runs
Java is built around one promise: write once, run anywhere. You write .java
source files in plain text. The compiler javac turns them into bytecode
(.class files) — not machine code for your specific computer, but
instructions for the JVM (Java Virtual Machine). A JVM exists for Windows,
macOS, and Linux, and each one runs that same bytecode.
So the loop is: write → compile → run.
The write-compile-run loop: one source file, one bytecode file, any machine.
Your first program
Create a file named Hello.java:
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, Java!");
}
}
Reading it line by line:
public class Hello— Java code lives inside classes. The file name must match the public class name, so this file must beHello.java.public static void main(String[] args)— the entry point. When you run the program, the JVM looks for exactly this method and starts there.System.out.println(...)— prints a line of text to the console.
Compile and run from a terminal in that folder:
javac Hello.java
java Hello
Output
Hello, Java!
Note it is java Hello, not java Hello.class — you name the class, not
the file.
A class file is a recipe card and the JVM is the cook. You hand over the recipe's name, not the card's filename.
One modern aside: for a single file, java Hello.java also works — the JVM
compiles it in memory. We teach the two-step loop because every real project
uses it.
Try it
- run it: create
Hello.java, compile it, run it, see the greeting. - tweak it: change the message to your own name. Predict which of the two commands you must repeat, then check.
- break it: rename the class to
Helobut leave the file name alone, then compile. Read the error slowly — it names the exact problem. - build it: print three lines about yourself using three
printlncalls.
Check yourself
1. What does
javacproduce, and what actually runs that output?2. Why must this file be named
Hello.java?3. Predict the problem: you run
java Hello.classand get an error. Why, and what is the correct command?
My notes
Yours alone, kept in this browser.