Command Palette

Search for a command to run...

Back to the lesson: Topic 0.3 — Java Versions, LTS Releases and the Version Tags in This Course
Core Java · Example 2 of 3 Java 21+

Java 21 features: record patterns and sequenced collections

Needs JDK 21 or newer on your machine. Sealed interfaces (17), record patterns in switch (21) and getFirst/getLast/reversed (21).

A new Java version comes out every March and September, and every two years one of them is a Long-Term Support (LTS) release that companies stay on for years: Java 8, 11, 17, 21 and 25. This course tags each feature with the version that introduced it (such as Java 16), and the in-browser Run button uses Java 17.

Change the code and press Run (Ctrl+Enter). Try to predict the output first, then break it on purpose and read the error. Your edits are saved and match the lesson page.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Rewrite this Java 14 switch expression as an old-style if/else chain that works on Java 8: String size = switch (n) { case 0 -> "none"; case 1 -> "one"; default -> "many"; }; with n = 1, and print the result.

02

Write a program that uses "ab".repeat(3) (Java 11) and " hi ".strip() (Java 11), and prints both results inside square brackets.

03

Write a program that prints whether the running Java has virtual threads (Java 21) using Runtime.version().feature(). What does it print in the browser?

Explain it without notes

01

What does LTS mean, and why do most companies run an LTS version?

02

List the five LTS versions and one headline feature for each.

03

What is a preview feature, and how do you use one?

04

What does javac --release 17 do, and why is it better than just compiling with the newest JDK?

05

What does a Java 21 badge on a code example in this course mean for you?

Java 21 features: record patterns and sequenced collections Java 21+
This example needs Java 21+. The in-browser compiler is Java 17: install JDK 21 or newer and run it with "java Main.java".

Expected output

a circle of radius 2.0
a square of side 3.0
a big circle
First: Circle[radius=2.0]
Last: Circle[radius=12.0]
Reversed: [Circle[radius=12.0], Square[side=3.0], Circle[radius=2.0]]