Overview of New Features in Java 20

Introduction Java 20 was released on 21st March 2023, six months before the Java 21Click to read tutorial: Overview of New Features in Java 21 long-term support release. All seven of its JDK Enhancement Proposals (JEPs) were previews or incubators: Java 20 was the final round of feedback for the features which Java 21 would make permanent, plus the first appearance of scoped values. This article gives an overview of the new features in Java 20, explains how its previews changed from Java 19, and lists the smaller final changes, with links to the detailed tutorials of the final versions. Preview and incubator features
  1. Scoped Values (Incubator) - JEP 429 - a new way to share immutable data with code called by a thread, and with its child threads, designed as a safer and cheaper alternative to ThreadLocal, especially with virtual threads. Final in Java 25 - read the scoped values tutorialClick to read: scoped values tutorial.
  2. Virtual Threads (Second Preview) - JEP 436 - mostly unchanged from Java 19; a few small APIs were made permanent separately. Final in Java 21 - read the virtual threads tutorialClick to read tutorial: Java 21 Virtual Threads Tutorial with Examples.
  3. Record Patterns (Second Preview) - JEP 432 - added type inference for the type arguments of generic record patterns and record patterns in enhanced for loops (the latter was dropped again before Java 21). Read about record patternsClick to read tutorial: Java 21 Record Patterns with Examples.
  4. Pattern Matching for switch (Fourth Preview) - JEP 433 - simplified the grammar of switch labels and made an exhaustive switch over an enum throw MatchException when an unexpected value appears at runtime. Read about pattern matching for switchClick to read tutorial: Java 21 Pattern Matching for switch with Examples.
  5. Structured Concurrency (Second Incubator) - JEP 437 - subtasks forked in a scope now inherit its scoped values.
  6. Foreign Function & Memory API (Second Preview) - JEP 434 - unified the memory segment and address abstractions. Final in Java 22 - read the Foreign Function & Memory API tutorialClick to read: Foreign Function & Memory API tutorial.
  7. Vector API (Fifth Incubator) - JEP 438 - continued incubation of the SIMD Vector API.
Final changes
  • URL constructors deprecated: the java.net.URL constructors were deprecated, because they do not validate their input; use URI.create(...).toURL() instead.
  • Thread.stop() disabled: Thread.stop() now always throws UnsupportedOperationException, as suspend() and resume() already did. These methods were unsafe, and had been deprecated since Java 1.2.
  • Named regex groups: Matcher.namedGroups() and Pattern.namedGroups() return the names and numbers of the named groups in a regular expression.
  • Lossy conversion warnings: javac -Xlint:lossy-conversions warns about compound assignments which silently narrow a value, such as int i; i += 1.5;.
Summary In this article we went through the new features of Java 20. It introduced scoped values as an incubator, and gave virtual threads, record patterns, pattern matching for switch, structured concurrency, the Foreign Function & Memory API and the Vector API one more round of preview or incubation before Java 21. Among the final changes, the URL constructors were deprecated, Thread.stop() was disabled, regular expressions gained namedGroups(), and javac learned to warn about lossy conversions. Next, read the overview of new features in Java 21Click to read tutorial: Overview of New Features in Java 21.