Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท Language Features by Version

Java 18 to 21 features

What arrived in Java 18, 19, 20 and 21 (LTS): UTF-8 by default, virtual threads, record patterns, pattern matching for switch and sequenced collections.

Java 18 to 21 is the stretch where Java's "modern" style settled: pattern matching grew from a single instanceof form into a full data-oriented toolkit, virtual threads made blocking code cheap, and the collections framework finally got a notion of order. Java 21 (September 2023) is a Long-Term Support release, so many teams moved straight from 17 to 21 and met all of this at once.

This page lists what shipped in each release, which features were preview on the way (and are final now), and shows the features that matter most in day-to-day code. Everything in the runnable examples is final in Java 21 and later, so it runs on our Java 25 runner without any flags.

Java
// Java 21: a switch that takes records apart and is checked for completeness
String describe(Shape s) {
    return switch (s) {
        case Circle(double r)         -> "circle " + r;
        case Rect(double w, double h) -> "rect " + w + "x" + h;
    };
}

Try it

Java Record patterns and pattern matching for switch

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java Sequenced collections and virtual threads

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Release by release

Java 18 (March 2022)

  • UTF-8 is the default charset for Charset.defaultCharset(), FileReader, Files.readString and friends, no matter what the operating system says. native.encoding still reports the platform encoding.
  • Simple Web Server: the jwebserver command and SimpleFileServer class serve a directory over HTTP for quick tests.
  • @snippet in Javadoc for compiled, checkable code samples (Javadoc).
  • Object.finalize() was deprecated for removal; use try-with-resources or Cleaner.

Java 19 (September 2022)

Mostly previews: virtual threads, record patterns, pattern matching for switch (third preview) and the foreign function and memory API. API additions that are final include Thread.threadId() (replacing the deprecated getId()) and Locale.of(...) factories.

Java 20 (March 2023)

Second previews of virtual threads, record patterns and switch patterns, plus the first incubation of scoped values. Nothing to adopt in production code.

Java 21 (September 2023, LTS)

  • Virtual threads are final (virtual threads).
  • Record patterns and pattern matching for switch are final (pattern matching, switch).
  • Sequenced collections: new interfaces SequencedCollection, SequencedSet and SequencedMap add getFirst(), getLast(), addFirst(), addLast(), removeFirst(), removeLast() and reversed() to List, Deque, LinkedHashSet, LinkedHashMap, TreeSet, TreeMap and others.
  • Generational ZGC (enabled with -XX:+UseZGC -XX:+ZGenerational; the default mode of ZGC from Java 23).
  • Small additions: Math.clamp, StringBuilder.repeat, String.indexOf(ch, from, to), String.splitWithDelimiters, Character.isEmoji.
  • Preview in 21 that are final in 22 or later: unnamed patterns and variables, string templates (later withdrawn), unnamed classes and instance main methods, scoped values, structured concurrency.

Usage notes

  • A switch over a sealed hierarchy needs no default: the compiler checks that every permitted subtype is handled, and adding a new subtype later turns missing cases into compile errors.
  • In a pattern switch, put more specific case labels (and when guards) before general ones. A label that can never match is a compile error ("dominated").
  • case null is only needed when null should be handled; without it a switch on null still throws NullPointerException.
  • List.reversed() is a view, not a copy: changes to one show in the other. List.getFirst() on an empty list throws NoSuchElementException, unlike get(0)'s IndexOutOfBoundsException.
  • Virtual threads help blocking, I/O-bound work. They do not make CPU-bound code faster. Do not pool them; create one per task.
  • Code written for Java 21 will not compile on 17, because of pattern switch, record patterns and sequenced methods. Set --release 21 (or the build tool's equivalent) to be told early.