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 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
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.
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.readStringand friends, no matter what the operating system says.native.encodingstill reports the platform encoding. - Simple Web Server: the
jwebservercommand andSimpleFileServerclass serve a directory over HTTP for quick tests. @snippetin Javadoc for compiled, checkable code samples (Javadoc).Object.finalize()was deprecated for removal; usetry-with-resources orCleaner.
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
switchare final (pattern matching, switch). - Sequenced collections: new interfaces
SequencedCollection,SequencedSetandSequencedMapaddgetFirst(),getLast(),addFirst(),addLast(),removeFirst(),removeLast()andreversed()toList,Deque,LinkedHashSet,LinkedHashMap,TreeSet,TreeMapand 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 specificcaselabels (andwhenguards) before general ones. A label that can never match is a compile error ("dominated"). case nullis only needed whennullshould be handled; without it aswitchonnullstill throwsNullPointerException.List.reversed()is a view, not a copy: changes to one show in the other.List.getFirst()on an empty list throwsNoSuchElementException, unlikeget(0)'sIndexOutOfBoundsException.- 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.