← Back to Java series
β˜•
Basics
Basics Β· Prerequisite: none

01. Getting Started with Java

Java is a statically-typed, object-oriented programming language that has been an industry workhorse since its 1995 release. In this lecture you'll install JDK 21 LTS, pick an IDE, and compile + run your first program.

JavaJDKbasicsgetting started
Duration
⏱ ~1-1.5 hours
Level
πŸ“Š Beginner
Prerequisite
🎯 None
OUTCOME
Java is a statically-typed, object-oriented programming language that has been an industry workhorse since its 1995 release. In this lecture you'll install JDK 21 LTS, pick an IDE, and compile + run your first program.

What you'll learn

  • 1Install JDK 21 LTS and verify the `java` / `javac` commands
  • 2Set up either IntelliJ IDEA Community or VS Code
  • 3Understand the structure of `public class` and `public static void main`
  • 4Compile with `javac` and run with `java`

Overview

Java is a statically-typed, object-oriented programming language that has been an industry workhorse since its 1995 release. In this lecture you'll install JDK 21 LTS, pick an IDE, and compile + run your first program.

Core Concepts

1) JDK and JRE

To write and run Java code you need the **JDK (Java Development Kit)**, which bundles the compiler (`javac`), the launcher (`java`), the standard library, and the JRE (Java Runtime Environment). We recommend **Eclipse Temurin JDK 21 LTS**.

bash
java --version    # openjdk 21.x.x
javac --version   # javac 21.x.x

2) Installing JDK 21 by OS

Download page: https://adoptium.net/temurin/releases/?version=21

Windows

  1. On the Temurin site, download `OpenJDK 21 - Windows x64 .msi` and double-click to install
  2. In the installer wizard, check "Set JAVA_HOME variable" and "Add to PATH"
  3. Default install path: `C:\Program Files\Eclipse Adoptium\jdk-21.x.x-hotspot\`
  4. Open a fresh PowerShell and verify with `$env:JAVA_HOME` and `java --version`
  5. If manual setup is needed: System Properties β†’ Advanced β†’ Environment Variables, set `JAVA_HOME` and add `%JAVA_HOME%\bin` to `Path`
powershell
$env:JAVA_HOME
# C:\Program Files\Eclipse Adoptium\jdk-21.x.x-hotspot
java --version

macOS

Option A β€” Homebrew (recommended):

bash
brew install --cask temurin@21

Install path: `/Library/Java/JavaVirtualMachines/temurin-21.jdk/Contents/Home`

Add to `~/.zshrc` then `source ~/.zshrc`:

bash
export JAVA_HOME=$(/usr/libexec/java_home -v 21)
export PATH="$JAVA_HOME/bin:$PATH"

Option B β€” download `.pkg` from the Temurin site (installs to the same path).

Linux (Ubuntu/Debian)

Option A β€” OpenJDK 21 via apt:

bash
sudo apt update
sudo apt install -y openjdk-21-jdk

Install path: `/usr/lib/jvm/java-21-openjdk-amd64`

Option B β€” Adoptium Temurin repository:

bash
# Add key and repository
sudo apt install -y wget apt-transport-https
wget -qO - https://packages.adoptium.net/artifactory/api/gpg/key/public \
  | sudo gpg --dearmor -o /etc/apt/keyrings/adoptium.gpg
echo "deb [signed-by=/etc/apt/keyrings/adoptium.gpg] \
  https://packages.adoptium.net/artifactory/deb $(lsb_release -cs) main" \
  | sudo tee /etc/apt/sources.list.d/adoptium.list

sudo apt update
sudo apt install -y temurin-21-jdk

Install path: `/usr/lib/jvm/temurin-21-jdk-amd64`

In `~/.bashrc` (or `~/.zshrc`):

bash
export JAVA_HOME=/usr/lib/jvm/temurin-21-jdk-amd64
export PATH="$JAVA_HOME/bin:$PATH"

Verifying the install (all OS)

bash
java --version
# openjdk 21.x.x or temurin 21.x.x

javac --version
# javac 21.x.x

echo $JAVA_HOME      # macOS / Linux
# /Library/Java/... or /usr/lib/jvm/...

If both `java` and `javac` report 21, you're ready.

3) Structure of `Hello.java`

java
public class Hello {
    public static void main(String[] args) {
        System.out.println("Hello, Java!");
    }
}
  • The filename must match the `public class` name β†’ `Hello.java`
  • `main(String[] args)` is the **entry point**
  • `System.out.println(...)` prints one line and adds a newline

4) Compile and run

bash
javac Hello.java   # produces Hello.class
java Hello         # run (no extension!)

`javac` converts `.java` source into **bytecode** `.class`, and `java` runs that bytecode on the JVM.

5) Choosing an IDE

ToolStrengths
IntelliJ IDEA CommunityMost popular for Java/Spring, free, powerful refactoring
VS Code + Extension Pack for JavaLightweight, multipurpose, great for polyglot work

This course works equally well with either tool.

6) Installing the IDE by OS

IntelliJ IDEA Community Edition

Download page: https://www.jetbrains.com/idea/download/?section=

**Windows**

  1. On JetBrains' site, choose the "Community Edition" tab and download the `.exe` installer
  2. Double-click and follow the wizard (recommended: 64-bit launcher / Add bin folder to PATH)
  3. Default install path: `C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2025.x\`
  4. User config (project caches/indexes): `C:\Users\<user>\AppData\Roaming\JetBrains\IdeaIC2025.x\`

**macOS**

Option A β€” Homebrew:

bash
brew install --cask intellij-idea-ce

Option B β€” download the `.dmg` from JetBrains and drag.

Install path: `/Applications/IntelliJ IDEA CE.app`

User config: `~/Library/Application Support/JetBrains/IdeaIC2025.x/`

**Linux (Ubuntu/Debian)**

Option A β€” Snap (easiest):

bash
sudo snap install intellij-idea-community --classic

Install path: `/snap/intellij-idea-community/current/`

Option B β€” extract the `.tar.gz` from JetBrains into `/opt`:

bash
sudo tar -xzf ideaIC-2025.x.tar.gz -C /opt
sudo ln -sf /opt/idea-IC-*/bin/idea.sh /usr/local/bin/idea
idea  # run

User config: `~/.config/JetBrains/IdeaIC2025.x/`

VS Code + Extension Pack for Java

Download page: https://code.visualstudio.com/Download

**Windows**

  1. Download the `.exe` (User Installer recommended) and install
  2. Default install path (User Installer): `C:\Users\<user>\AppData\Local\Programs\Microsoft VS Code\`
  3. For System Installer: `C:\Program Files\Microsoft VS Code\`

**macOS**

Option A β€” Homebrew:

bash
brew install --cask visual-studio-code

Option B β€” download the `.zip` from the site, unzip, and drag to `Applications`.

Install path: `/Applications/Visual Studio Code.app`

**Linux (Ubuntu/Debian)**

Option A β€” Microsoft `.deb`:

bash
sudo apt install -y wget gpg apt-transport-https
wget -qO- https://packages.microsoft.com/keys/microsoft.asc \
  | gpg --dearmor | sudo tee /etc/apt/keyrings/packages.microsoft.gpg > /dev/null
echo "deb [arch=amd64 signed-by=/etc/apt/keyrings/packages.microsoft.gpg] \
  https://packages.microsoft.com/repos/code stable main" \
  | sudo tee /etc/apt/sources.list.d/vscode.list

sudo apt update
sudo apt install -y code

Install path: `/usr/share/code/`, executable at `/usr/bin/code`.

Option B β€” Snap:

bash
sudo snap install --classic code

Java extension pack (all OS)

Open VS Code, click the Extensions icon on the left (`Ctrl+Shift+X`), search for `Extension Pack for Java` (by Microsoft) and install. Or via CLI:

bash
code --install-extension vscjava.vscode-java-pack

Includes:

  • Language Support for Java (Red Hat)
  • Debugger for Java
  • Test Runner for Java
  • Maven for Java
  • Project Manager for Java
πŸ’‘

Note: VS Code's Java extension uses the system `JAVA_HOME` directly. If you've set `JAVA_HOME` to 21 after installing the JDK, no extra IDE config is needed.

Examples

Example 1 β€” `Hello.java`: the smallest Java program

java
public class Hello {
    public static void main(String[] args) {
        System.out.println("Hello, Java!");
    }
}

**Output**

text
Hello, Java!

**Note:** if the filename doesn't match the class name you get the compile error `class Hello is public, should be declared in a file named Hello.java`.

Example 2 β€” `Args.java`: command-line arguments

java
public class Args {
    public static void main(String[] args) {
        if (args.length == 0) {
            System.out.println("Try passing a name as an argument.");
            return;
        }
        System.out.println("Hello, " + args[0] + "!");
    }
}

**Output**

text
$ java Args Java
Hello, Java!

**Note:** `args` is the parameter of `main`. `args.length` gives the count, and you index into it like `args[0]`.

Example 3 β€” `PrintFormats.java`: print / println / printf

java
public class PrintFormats {
    public static void main(String[] args) {
        System.out.print("no ");
        System.out.print("newline\n");
        System.out.println("auto newline");
        System.out.printf("name=%s, age=%d%n", "Jisoo", 21);
    }
}

**Output**

text
no newline
auto newline
name=Jisoo, age=21

**Note:** `printf`'s `%n` uses the OS-appropriate line separator; `\n` always emits LF.

Example 4 β€” `CommentsAndJavadoc.java`: three kinds of comments

java
/** Demo of the three comment styles. */
public class CommentsAndJavadoc {
    // single-line comment
    /* multi-line
       comment */
    public static void main(String[] args) {
        System.out.println(greeting("Java")); // inline comment
    }

    /** Return a greeting. */
    static String greeting(String name) {
        return "Hello, " + name + "!";
    }
}

**Output**

text
Hello, Java!

**Note:** `/** ... */` is a **Javadoc** comment β€” the official API comment format that the `javadoc` tool turns into HTML documentation.

Full example code (src/)

src/Args.java

java
public class Args {
    public static void main(String[] args) {
        if (args.length == 0) {
            System.out.println("Try passing a name as an argument.");
            return;
        }
        System.out.println("Hello, " + args[0] + "!");
    }
}

src/CommentsAndJavadoc.java

java
/** Demo of the three comment styles. */
public class CommentsAndJavadoc {
    // single-line comment
    /* multi-line
       comment */
    public static void main(String[] args) {
        System.out.println(greeting("Java")); // inline comment
    }

    /** Return a greeting. */
    static String greeting(String name) {
        return "Hello, " + name + "!";
    }
}

src/Hello.java

java
public class Hello {
    public static void main(String[] args) {
        System.out.println("Hello, Java!");
    }
}

src/PrintFormats.java

java
public class PrintFormats {
    public static void main(String[] args) {
        System.out.print("no ");
        System.out.print("newline\n");
        System.out.println("auto newline");
        System.out.printf("name=%s, age=%d%n", "Jisoo", 21);
    }
}

Common Mistakes

  1. Saving the file as `hello.java` (lowercase) so it doesn't match the class name
  2. Running `java Hello.class` with the extension β†’ use `java Hello`
  3. Changing `main`'s signature to `void main()` and losing the entry point
  4. Dropping the `out` from `System.out.println` and writing `System.println`
  5. Garbled output when printing non-ASCII: save the file as UTF-8 and use a UTF-8 console (`chcp 65001` on Windows)

Summary

  • A Java program starts at `public static void main(String[] args)` inside some `public class Xxx`
  • Compile a `.java` to a `.class` with `javac`, then run it with `java`
  • Pick between `System.out.print` / `println` / `printf` to suit the situation

Practice

# Practice - 01. Getting Started with Java

## Exercise 1 β€” Print a self-introduction

  • File: `Homework01.java`
  • Key concepts: `public class`, `main`, `System.out.println`

Requirements

  • Print your name, favorite language, and start year, each on its own line.
  • The first line must be `=== Self Introduction ===`.

Expected output

text
=== Self Introduction ===
Name: John Doe
Favorite language: Java
Started in: 2026

Hint

  • Just call `System.out.println` four times.

## Exercise 2 β€” Command-line greeting

  • File: `Homework02.java`
  • Key concepts: the `args` array, conditional branching

Requirements

  • If the command-line argument is empty, print `Usage: java Homework02 <name>` and exit.
  • If a first argument is given, print `Hello, <name>!`.

Expected output

text
$ java Homework02
Usage: java Homework02 <name>

$ java Homework02 Jisoo
Hello, Jisoo!

Hint

  • Use `args.length` to check how many arguments were passed.

## Solutions After trying it yourself, compare with [`answer/`](./answer/). The answer files include key points and common pitfalls as comments.

Solution code (homework/answer/)

answer/Homework01.java

java
/** Print a 4-line self-introduction. */
public class Homework01 {
    public static void main(String[] args) {
        System.out.println("=== Self Introduction ===");
        System.out.println("Name: John Doe");
        System.out.println("Favorite language: Java");
        System.out.println("Started in: 2026");
    }
}

answer/Homework02.java

java
/** Greet based on a command-line argument. */
public class Homework02 {
    public static void main(String[] args) {
        if (args.length == 0) {
            System.out.println("Usage: java Homework02 <name>");
            return;
        }
        System.out.println("Hello, " + args[0] + "!");
    }
}

Try It Yourself

bash
cd 01_basics/01_getting_started/src
javac Hello.java
java Hello

Same pattern for the others:

bash
javac Args.java
java Args CodingNow

Next Lecture

[02_Variables_and_Types](../02_λ³€μˆ˜μ™€_νƒ€μž…/) β€” primitives, type conversion, and the `var` keyword.

Example code / lecture materials

All lecture materials and example code are openly available on GitHub.

View on GitHub β†—