Saltar a contenido

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 .pkg installer. Or brew install --cask temurin if you use Homebrew.
  • Linux: sudo apt install openjdk-25-jdk on Debian/Ubuntu, or download from Adoptium.
  • Windows: download the .msi from Adoptium and run it. Make sure the installer adds Java to your PATH.

Open a terminal:

  • macOS: ⌘ Space → "terminal".
  • Windows: Windows key → "powershell".
  • Linux: you know how.

Check:

java --version
javac --version

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

mkdir -p ~/code/java-learning
cd ~/code/java-learning

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:

javac Hello.java

That creates Hello.class (the bytecode). Run:

java Hello

Note: java Hello (no .class, no .java). You'll see:

hello, world

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:

java Hello.java

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 called Hello. Java is built around classes. Every program starts with at least one. public means "visible from outside this file."
  • public static void main(String[] args) { - defines a method called main. This is special: java Hello looks 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 explain static properly when we meet objects in page 05. For now: main has to be static.)
  • void - returns nothing.
  • String[] args - receives any command-line arguments as an array of strings.
  • System.out.println("hello, world"); - calls println on System.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:

  1. Change "hello, world" to your name. Re-compile and re-run.

  2. Add a second println:

    System.out.println("hello, world");
    System.out.println("this is my second line");
    

  3. Print a number:

    System.out.println(42);
    

  4. Break it. Remove a semicolon. Run javac - read the error.

  5. Restore. Mistype Println (capital P). Run javac - 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.

jshell

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.

Next: First real program →

Comments