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.
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**.
java --version # openjdk 21.x.x
javac --version # javac 21.x.x2) Installing JDK 21 by OS
Download page: https://adoptium.net/temurin/releases/?version=21
Windows
- On the Temurin site, download `OpenJDK 21 - Windows x64 .msi` and double-click to install
- In the installer wizard, check "Set JAVA_HOME variable" and "Add to PATH"
- Default install path: `C:\Program Files\Eclipse Adoptium\jdk-21.x.x-hotspot\`
- Open a fresh PowerShell and verify with `$env:JAVA_HOME` and `java --version`
- If manual setup is needed: System Properties β Advanced β Environment Variables, set `JAVA_HOME` and add `%JAVA_HOME%\bin` to `Path`
$env:JAVA_HOME
# C:\Program Files\Eclipse Adoptium\jdk-21.x.x-hotspot
java --versionmacOS
Option A β Homebrew (recommended):
brew install --cask temurin@21Install path: `/Library/Java/JavaVirtualMachines/temurin-21.jdk/Contents/Home`
Add to `~/.zshrc` then `source ~/.zshrc`:
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:
sudo apt update
sudo apt install -y openjdk-21-jdkInstall path: `/usr/lib/jvm/java-21-openjdk-amd64`
Option B β Adoptium Temurin repository:
# 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-jdkInstall path: `/usr/lib/jvm/temurin-21-jdk-amd64`
In `~/.bashrc` (or `~/.zshrc`):
export JAVA_HOME=/usr/lib/jvm/temurin-21-jdk-amd64
export PATH="$JAVA_HOME/bin:$PATH"Verifying the install (all OS)
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`
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
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
| Tool | Strengths |
|---|---|
| IntelliJ IDEA Community | Most popular for Java/Spring, free, powerful refactoring |
| VS Code + Extension Pack for Java | Lightweight, 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**
- On JetBrains' site, choose the "Community Edition" tab and download the `.exe` installer
- Double-click and follow the wizard (recommended: 64-bit launcher / Add bin folder to PATH)
- Default install path: `C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2025.x\`
- User config (project caches/indexes): `C:\Users\<user>\AppData\Roaming\JetBrains\IdeaIC2025.x\`
**macOS**
Option A β Homebrew:
brew install --cask intellij-idea-ceOption 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):
sudo snap install intellij-idea-community --classicInstall path: `/snap/intellij-idea-community/current/`
Option B β extract the `.tar.gz` from JetBrains into `/opt`:
sudo tar -xzf ideaIC-2025.x.tar.gz -C /opt
sudo ln -sf /opt/idea-IC-*/bin/idea.sh /usr/local/bin/idea
idea # runUser config: `~/.config/JetBrains/IdeaIC2025.x/`
VS Code + Extension Pack for Java
Download page: https://code.visualstudio.com/Download
**Windows**
- Download the `.exe` (User Installer recommended) and install
- Default install path (User Installer): `C:\Users\<user>\AppData\Local\Programs\Microsoft VS Code\`
- For System Installer: `C:\Program Files\Microsoft VS Code\`
**macOS**
Option A β Homebrew:
brew install --cask visual-studio-codeOption 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`:
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 codeInstall path: `/usr/share/code/`, executable at `/usr/bin/code`.
Option B β Snap:
sudo snap install --classic codeJava 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:
code --install-extension vscjava.vscode-java-packIncludes:
- 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
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, Java!");
}
}**Output**
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
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**
$ 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
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**
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
/** 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**
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
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
/** 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
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, Java!");
}
}
src/PrintFormats.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
- Saving the file as `hello.java` (lowercase) so it doesn't match the class name
- Running `java Hello.class` with the extension β use `java Hello`
- Changing `main`'s signature to `void main()` and losing the entry point
- Dropping the `out` from `System.out.println` and writing `System.println`
- 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
=== Self Introduction ===
Name: John Doe
Favorite language: Java
Started in: 2026Hint
- 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
$ 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
/** 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
/** 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
cd 01_basics/01_getting_started/src
javac Hello.java
java HelloSame pattern for the others:
javac Args.java
java Args CodingNowNext Lecture
[02_Variables_and_Types](../02_λ³μμ_νμ /) β primitives, type conversion, and the `var` keyword.
All lecture materials and example code are openly available on GitHub.
View on GitHub β