Java · lesson 2 of 66
Beginner
17- 1 Introduction to Java
- 2 Setting Up Java
- 3 Data Types in Java
- 4 Operators in Java
- 5 Variables and Operators in Java
- 6 Control Flow in Java
- 7 Arrays in Java
- 8 Functions (Methods) in Java
- 9 Strings in Java
- 10 Methods in Java
- 11 Java OOP — Complete Guide
- 12 Introduction to Object-Oriented Programming in Java
- 13 Encapsulation in Java
- 14 Inheritance in Java
- 15 Polymorphism in Java
- 17 Abstraction in Java
- 18 Error Types in Java
Intermediate
38- 16 Regular Expressions in Java
- 19 Interfaces in Java — A Deep Dive
- 20 Abstract Classes in Java
- 21 Method Overloading in Java
- 22 Composition vs Inheritance in Java
- 23 Generics in Java
- 27 Exception Handling in Java
- 28 Java Streams API
- 29 Collections Framework in Java
- 30 Optional in Java
- 31 File Handling in Java
- 32 Records in Java
- 33 Java 8 Features
- 34 Sealed Classes and Interfaces in Java
- 35 Multithreading in Java
- 37 JDBC — Database Access in Java
- 39 Maven — Java Build and Dependency Management
- 42 Gradle — Modern Java Build Tool
- 44 Unit Testing with JUnit 5 and Mockito
- 45 Logging in Java with SLF4J and Logback
- 49 Spring Boot — Introduction
- 50 Dependency Injection in Spring Boot
- 51 Java Project Ideas
- 52 Building REST APIs with Spring Boot
- 53 Validation in Spring Boot
- 54 Spring Data JPA — Database Access
- 55 Exception Handling in Spring Boot
- 56 Spring Security — Authentication and Authorization
- 57 JWT Authentication in Spring Boot
- 58 Dockerizing Spring Boot Applications
- 59 Java Enums: Beyond Simple Constants
- 60 Inner and Anonymous Classes in Java
- 61 Java Generics: Bounded Types, Wildcards, and Generic Methods
- 62 Java Date/Time API: LocalDate, LocalDateTime, and Duration
- 63 Java NIO.2: Paths, Files, and WatchService
- 64 Java Annotations: Custom Types and Meta-Information
- 65 Java Reflection: Dynamic Type Inspection and Method Invocation
- 66 Java Localization: ResourceBundle and Locale Support
Advanced
11Setting Up Java
Install the JDK, set up IntelliJ IDEA, write and run your first Java program, and understand how javac and the JVM turn source code into output.
By SkillByExample · Updated
Before writing Java, you need two things: a JDK to compile and run code, and an IDE to write it comfortably. Getting this setup right once means you’ll never fight your environment again — every project you create will just work.
Install the JDK
The JDK (Java Development Kit) includes the compiler (javac), the JVM (java), and the standard library. Without it, you cannot compile or run Java programs. Always install a Long-Term Support (LTS) release — these are maintained for years and are safe for both learning and production work.
Download: adoptium.net — choose Temurin 21 (LTS), the latest long-term support release.
Windows
- Download the
.msiinstaller for Windows x64 - Run it — the installer sets
JAVA_HOMEand addsjava/javacto yourPATHautomatically - Open a new terminal and verify. On the Linux machine used for this course it prints:
$ java -version
openjdk version "21.0.12.1" 2026-08-18
OpenJDK Runtime Environment (build 21.0.12.1+1-1-24.04.4-Ubuntu)
OpenJDK 64-Bit Server VM (build 21.0.12.1+1-1-24.04.4-Ubuntu, mixed mode, sharing)
$ javac -version
javac 21.0.12.1
Your version string and vendor (Temurin, Ubuntu’s OpenJDK, Oracle…) will differ; what matters is
that both commands answer and the major version is 21 or later. java -version writes to
standard error, which is why some scripts redirect it with 2>&1.
macOS
# Using Homebrew (recommended)
brew install --cask temurin@21
java -version # verify
Linux (Debian/Ubuntu)
sudo apt update
sudo apt install temurin-21-jdk # after adding the Adoptium repo
java -version
Set Up IntelliJ IDEA
A good IDE removes friction from the whole development cycle — it catches errors before you run, suggests completions, and lets you navigate large codebases instantly. IntelliJ IDEA Community Edition is free and the most widely used Java IDE in the industry, so learning it now maps directly to professional workflows.
- Download from jetbrains.com/idea — choose Community Edition
- Install and launch it
- On the welcome screen: New Project
- Choose Java, select your JDK (21), click Next
- Give your project a name —
HelloJava— and click Create
IntelliJ detects the JDK you installed automatically. If it doesn’t, click Add JDK and point it to your installation directory.
Your First Java Program
Writing and running “Hello, World!” confirms the whole toolchain works. In IntelliJ: right-click
src → New → Java Class → name it Hello, then press the green Run button
(Shift+F10). From a terminal it looks like this:
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
$ javac Hello.java
$ ls
Hello.class
Hello.java
$ java Hello
Hello, World!
How Compilation Works
Java runs in two steps: javac compiles source to platform-neutral bytecode in a .class
file, and the java launcher starts a JVM that loads and executes it. The same .class file runs
on Windows, macOS and Linux.
Hello.java --javac--> Hello.class --java--> JVM runs it on your OS
You can look inside the bytecode with javap, which ships with the JDK:
$ javac Hello.java
$ javap -c Hello
Compiled from "Hello.java"
public class Hello {
public Hello();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]);
Code:
0: getstatic #7 // Field java/lang/System.out:Ljava/io/PrintStream;
3: ldc #13 // String Hello, World!
5: invokevirtual #15 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
8: return
}
The compiler added a constructor you never wrote (public Hello()), and main became four JVM
instructions: fetch System.out, load the string, call println, return. Since Java 11,
java Hello.java also compiles and runs a single file in one step, in memory — the form used in
the rest of this course.
Anatomy of the Hello World Program
public class Hello { // class name must match the file name
public static void main(String[] args) { // the entry point Java looks for
System.out.println("Hello!"); // print a line to standard output
}
}
public class Hello— a public class must live in a file with the same name,Hello.javapublic static void main(String[] args)— where execution starts;argsholds command-line argumentsSystem.out.println(...)prints with a newline;System.out.print(...)prints without one- statements end with
;and blocks are wrapped in{}
IntelliJ Shortcuts Worth Learning Now
Learning a handful of keyboard shortcuts pays back immediately — you’ll spend less time navigating and more time thinking. These are the ones you’ll use every single day.
| Action | Windows/Linux | macOS |
|---|---|---|
| Run program | Shift+F10 | ⌃R |
| Run current file | Ctrl+Shift+F10 | ⌃⇧R |
| Auto-complete | Ctrl+Space | ⌃Space |
| Quick fix | Alt+Enter | ⌥Enter |
| Reformat code | Ctrl+Alt+L | ⌘⌥L |
| Find in file | Ctrl+F | ⌘F |
| Search everywhere | Shift+Shift | Shift+Shift |
Common First-Time Errors
These trip up almost every beginner. Each one below is the real message from JDK 21.
File name doesn’t match the public class
public class Greeter {
public static void main(String[] args) {
System.out.println("Hi");
}
}
$ javac Hello.java
Hello.java:1: error: class Greeter is public, should be declared in a file named Greeter.java
public class Greeter {
^
1 error
Saved as Hello.java, the class must be called Hello — capitalisation included.
main is missing static
public class Hello {
public void main(String[] args) {
System.out.println("Hello, World!");
}
}
$ javac Hello.java
$ java Hello
Error: Main method is not static in class Hello, please define the main method as:
public static void main(String[] args)
It compiles — main is a legal method name — but the launcher refuses to start it. The signature
must be exactly public static void main(String[] args).
Running with the .class extension
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
$ javac Hello.java
$ java Hello.class
Error: Could not find or load main class Hello.class
Caused by: java.lang.ClassNotFoundException: Hello.class
java takes a class name (Hello), not a file name. Only .java source files may be passed by
file name.
‘javac’ is not recognised / command not found
The JDK’s bin folder isn’t on your PATH. On Windows, re-run the installer with the “Set
JAVA_HOME / add to PATH” options, or add C:\Program Files\Eclipse Adoptium\jdk-21...\bin to
the system PATH and open a new terminal.
Project: Hello Java CLI App
A small program that greets the user and reports the environment it runs in.
import java.time.LocalDate;
public class HelloApp {
public static void main(String[] args) {
String name = args.length > 0 ? args[0] : "Java Developer";
System.out.println("=== Hello Java App ===");
System.out.println("Java version: " + System.getProperty("java.version"));
System.out.println("OS: " + System.getProperty("os.name"));
int birthYear = 2000;
int year = LocalDate.now().getYear();
System.out.println("Born in " + birthYear + ", you turn " + (year - birthYear) + " in " + year);
System.out.printf("Welcome, %s! Happy coding.%n", name);
}
}
$ java HelloApp.java Ada
=== Hello Java App ===
Java version: 21.0.12.1
OS: Linux
Born in 2000, you turn 26 in 2026
Welcome, Ada! Happy coding.
The version, operating system and year come from the machine and the clock, so your output will
differ in those lines. System.getProperty reads JVM properties; java -XshowSettings:properties -version lists them all.
Practice
- Print your JDK’s version, vendor and home folder using
System.getProperty.
Solution
public class JdkInfo {
public static void main(String[] args) {
for (String key : new String[] {"java.version", "java.vendor", "java.home"}) {
System.out.println(key + " = " + System.getProperty(key));
}
}
}
$ java JdkInfo.java
java.version = 21.0.12.1
java.vendor = Ubuntu
java.home = /usr/lib/jvm/java-21-openjdk-amd64
These three lines differ per machine; java.home is the folder JAVA_HOME should point to.
- Change
Helloto useprintinstead ofprintlntwice in a row. What does the output look like?
Solution
public class PrintVsPrintln {
public static void main(String[] args) {
System.out.print("Hello, ");
System.out.print("World!");
System.out.println();
System.out.println("next line");
}
}
$ java PrintVsPrintln.java
Hello, World!
next line
print adds no newline, so both pieces land on one line; the bare println() ends it.
- Compile
Hello.javainto a separateclassesfolder and run it from there.
Solution
public class Hello {
public static void main(String[] args) {
System.out.println("Hello from the classes folder");
}
}
$ javac -d classes Hello.java
$ java -cp classes Hello
Hello from the classes folder
$ java Hello
Error: Could not find or load main class Hello
Caused by: java.lang.ClassNotFoundException: Hello
-cp classes tells the JVM where to look; without it, java only searches the current folder,
where no Hello.class exists.
Next up: Data Types in Java — how Java stores and represents data.