Learn Java – tutorial, cheatsheet & exercises
Java has been one of the most important programming languages in the world for almost 30 years. The code is compiled to bytecode and runs on the Java Virtual Machine (JVM) – "Write once, run anywhere". Java is statically typed, object-oriented and, thanks to the garbage collector, memory-safe.
Modern Java (17 to 21 LTS) is much more compact than its reputation suggests: record, var, switch expressions, text blocks, pattern matching, sealed classes and virtual threads remove a lot of boilerplate. Java powers banks, insurance companies, Android apps, big-data systems (Kafka, Spark) and countless backends built with Spring Boot.
Hello World in Java
public class Main {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}Every Java program needs a class with main. Start it with java Main.java.
How to get started with Java
Install a JDK (Java Development Kit) in version 21 (LTS) – e.g. Temurin from adoptium.net, via brew install openjdk@21 on the Mac or winget install EclipseAdoptium.Temurin.21.JDK on Windows. Check the installation:
java --version
javac --versionSince Java 11 you can run a single source file directly without compiling it first. This is ideal for learning:
java Hallo.javaFor larger programs you compile with javac and then start the class (Hallo.java produces Hallo.class):
javac Hallo.java
java HalloFor quickly trying out individual lines there is the interactive shell jshell (included in the JDK). There you need neither a class nor main:
jshell
jshell> int x = 6 * 7
x ==> 42
jshell> "Java".toUpperCase()
$2 ==> "JAVA"
jshell> /exitJava learning path
Hello World, main & Running
Structure of a Java program, output and comments.
Variables & Data Types
Primitive types, String, var, conversion and typical number pitfalls.
Conditions, if & switch
Making decisions – including the modern switch expression.
Loops
for, for-each, while and do-while.
Methods
Reusable building blocks: parameters, return values, overloading, varargs.
Arrays, Lists, Maps & Sets
The most important data structures of the Collections Framework.
Strings, StringBuilder & Text Blocks
Processing and formatting text and writing multi-line text cleanly.
Classes, Inheritance & Interfaces
Object orientation: encapsulation, constructors, inheritance, abstract classes, interfaces.
Records & Enums
Immutable data classes without boilerplate and fixed sets of values.
Exceptions & Files
Catching errors, try-with-resources and reading and writing files with java.nio.
Lambdas & Streams
Declaratively filtering, transforming, grouping and summarizing data.
Optional & Generics
Handling null safely and writing type-safe, reusable classes and methods.
sealed, Pattern Matching & Record Patterns
Closed type hierarchies and expressive case distinctions checked by the compiler.
Concurrency & Virtual Threads
Threads, ExecutorService, CompletableFuture, atomics and virtual threads (Java 21).
JVM, Memory & Garbage Collection
How Java code runs, where objects live and typical reference pitfalls.
Build Tools, Modules & JUnit
Maven/Gradle, dependencies, JAR files and testing with JUnit 5.
At a glance
More about Java
Test & practice
Interactive quiz, timed exam with certificate, and exercises with an editor: