01 - Setup¶
What this session is¶
About 30 minutes. You'll install a modern Java Development Kit (JDK), open the terminal, write your first program, and meet JShell - Java's interactive scratchpad.
Step 1: Install a JDK¶
A JDK ("Java Development Kit") is the compiler + runtime + standard library, bundled. The current Long-Term-Support version is Java 25. Get any modern build:
- macOS: install Temurin (a free, open-source JDK build) via the
.pkginstaller. Orbrew install --cask temurinif you use Homebrew. - Linux:
sudo apt install openjdk-25-jdkon Debian/Ubuntu, or download from Adoptium. - Windows: download the
.msifrom Adoptium and run it. Make sure the installer adds Java to your PATH.
Open a terminal:
- macOS:
⌘ Space→ "terminal". - Windows:
Windowskey → "powershell". - Linux: you know how.
Check:
Both should print version numbers around 21 or later. If not, the install didn't work or PATH isn't set.
Step 2: Pick a folder for your code¶
Step 3: The simplest possible Java program¶
Create a file called Hello.java (note the capital H - Java requires the file name to match the class name).
Type this - don't copy-paste:
public class Hello {
public static void main(String[] args) {
System.out.println("hello, world");
}
}
Save it.
Step 4: Compile and run¶
Two steps. Compile:
That creates Hello.class (the bytecode). Run:
Note: java Hello (no .class, no .java). You'll see:
If something went wrong, the compiler tells you exactly which line. Read the message; fix; re-run.
Step 4b: Even simpler - run the source directly¶
Modern Java (11+) can compile-and-run a single file without producing a .class file:
That works for one-file programs. For projects with multiple files, you'll need a build tool (page 11).
What just happened - line by line¶
That five-line program has a lot of scaffolding. Let's walk through it:
public class Hello {- defines a class calledHello. Java is built around classes. Every program starts with at least one.publicmeans "visible from outside this file."public static void main(String[] args) {- defines a method calledmain. This is special:java Hellolooks for exactly this signature and runs it.public- visible from outside.static- belongs to the class itself, not to any object of the class. (We'll explainstaticproperly when we meet objects in page 05. For now:mainhas to be static.)void- returns nothing.String[] args- receives any command-line arguments as an array of strings.System.out.println("hello, world");- callsprintlnonSystem.out(the standard output) with"hello, world". The semicolon ends the statement.- The curly braces
{and}mark the boundaries of the class body and the method body.
Yes, this is a lot of scaffolding for one print. Java optimizes for predictability over brevity - every program has the same shape, so you always know where to look. You'll write public class X { public static void main(String[] args) { ... } } many times in the next few sessions; it'll become muscle memory.
Try changing things¶
The way to learn is to break things on purpose:
-
Change
"hello, world"to your name. Re-compile and re-run. -
Add a second
println: -
Print a number:
-
Break it. Remove a semicolon. Run
javac- read the error. -
Restore. Mistype
Println(capital P). Runjavac- read the error. Java is case-sensitive.
Reading errors is most of programming. Get comfortable seeing them.
JShell: instant gratification¶
Java's interactive scratchpad. Type lines, see results. No class boilerplate needed.
You'll see a jshell> prompt. Try:
jshell> 2 + 2
$1 ==> 4
jshell> "hello" + " " + "world"
$2 ==> "hello world"
jshell> System.out.println("hi")
hi
jshell> int x = 10
x ==> 10
jshell> x * 5
$5 ==> 50
Exit with /exit.
JShell is great for one-line experiments. "How does X behave?" → open JShell → try → see. Use it when you're stuck.
Use an IDE for real work¶
Compiling at the command line works for hello-world. For real projects, use an IDE (Integrated Development Environment):
- IntelliJ IDEA Community Edition (free) - the gold standard for Java. Inline error highlighting, refactoring, navigation, debugging - all excellent.
- VS Code with the "Extension Pack for Java" - lighter, runs everywhere, less Java-specific.
Set one up. For the rest of this path, you can use either the command line or the IDE - the IDE is much easier for anything past one file.
What you might wonder¶
"Why so much scaffolding for one print?"
Java's design ethic is "explicit > terse." Every Java file has the same shape, so you know where things live. Python's print("hi") is shorter but Java's structure scales better to large programs.
"Why do I have to put the class in a file named after the class?"
Convention enforced by the compiler. Makes finding code predictable: file Hello.java contains class Hello. The file holds (mostly) one public class.
"What's the difference between JDK and JRE?" JDK = compiler + tools + runtime (everything you need to develop). JRE = just the runtime (what you need to run compiled programs). For learning, install the JDK. JRE is for end-user deployment.
"Why System.out.println and not just println?"
println is a method on the out field of the System class. There's no global println function in Java; everything lives inside a class. You'll get a sense for why this matters in page 05.
Done¶
You have: - A JDK installed. - A folder for your code. - One working program. - JShell as a scratchpad. - An IDE installed (or chosen).
This was the infrastructure step. Next page is where the real learning starts.