Command Palette

Search for a command to run...

Phase 11Intermediate12 of 17 in Core Java

Modern Java

Pattern matching for instanceof and switch, record patterns, unnamed variables, compact source files, and a map of every release from Java 8 to 25.

Java changed more between 2014 and 2025 than in the twenty years before. Since Java 10 a new version ships every six months, and many features first appear as previews you can try before they become permanent. This phase teaches the modern features that change how everyday Java code looks: asking "what shape is this value?" and taking it apart in one step.

You'll start with **pattern matching for instanceof (Java 16), move to pattern matching for switch and record patterns (Java 21), which together with sealed types (Topic 5.11) and records (Topic 4.9) give Java a safe, compiler-checked way to model data. Then come the newest pieces: unnamed variables and patterns** with _ (Java 22) and **compact source files with instance main methods** (Java 25), which let a first program be a single void main().

The last topic is a map of what changed in every release from Java 8 to Java 25, so you can read any codebase, know which version a feature needs, and plan an upgrade. Examples that need Java 21 are marked; Java 22 and 25 features are shown as code you run on your own JDK, because the in-browser runner uses Java 17.

0/6 · 0%
6 topics ~3 h 24 code blocks & diagrams
Start with the first topic
1
11.1

Pattern Matching for instanceof

Pattern matching for instanceof tests a value's type and, if it matches, gives you a ready-cast variable in the same step: if (o instanceof String s) { s.length(); }. It removes the repeated cast, and the compiler decides exactly where the new variable can be used.

33 min 4 code practice

2
11.2

Pattern Matching for switch

From Java 21, switch can test the type and shape of any object, not only constants: case Circle c -> ..., with when guards, case null, and a compiler that checks every possible type is handled. With sealed types it replaces long if/instanceof chains with one exhaustive, checked expression.

36 min 4 code practice

3
11.3

Record Patterns

A record pattern like Point(int x, int y) checks that a value is a Point and pulls out its components into variables in one step. Record patterns nest, so Line(Point(var x1, var y1), Point p2) matches and unpacks a whole tree of records at once, in instanceof and in switch.

34 min 4 code practice

4
11.4

Unnamed Variables and Patterns

From Java 22 you can write _ instead of a name for a variable or pattern you must declare but never use: catch (NumberFormatException _), (_, v) -> ..., case Point(var x, _). It tells readers and the compiler "this is deliberately ignored", and it can't be read by mistake.

34 min 4 code practice

5
11.5

Compact Source Files and Instance main Methods

From Java 25 a complete program can be just void main() { IO.println("Hello"); }: no class declaration, no public static, no String[] args. The compiler wraps the file in a hidden class, the launcher creates an object and calls main on it, and common java.base packages are imported automatically.

30 min 4 code practice

6
11.6

What Changed from Java 8 to Java 25

Java ships a new version every six months, and every two years one becomes a long-term support (LTS) release: 8, 11, 17, 21 and 25. This topic maps the important language, library, JVM and removal changes in each release from Java 8 to Java 25, so you can read any codebase, know what version a feature needs, and plan an upgrade.

38 min 4 code practice