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
Final changes
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
- 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. - 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.
- Record Patterns (Second Preview) - JEP 432 - added type inference for the type arguments of generic record patterns and record patterns in enhanced
forloops (the latter was dropped again before Java 21). Read about record patternsClick to read tutorial: Java 21 Record Patterns with Examples. - Pattern Matching for switch (Fourth Preview) - JEP 433 - simplified the grammar of switch labels and made an exhaustive switch over an enum throw
MatchExceptionwhen an unexpected value appears at runtime. Read about pattern matching for switchClick to read tutorial: Java 21 Pattern Matching for switch with Examples. - Structured Concurrency (Second Incubator) - JEP 437 - subtasks forked in a scope now inherit its scoped values.
- 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.
- Vector API (Fifth Incubator) - JEP 438 - continued incubation of the SIMD Vector API.
- URL constructors deprecated: the
java.net.URLconstructors were deprecated, because they do not validate their input; useURI.create(...).toURL()instead. - Thread.stop() disabled:
Thread.stop()now always throwsUnsupportedOperationException, assuspend()andresume()already did. These methods were unsafe, and had been deprecated since Java 1.2. - Named regex groups:
Matcher.namedGroups()andPattern.namedGroups()return the names and numbers of the named groups in a regular expression. - Lossy conversion warnings:
javac -Xlint:lossy-conversionswarns about compound assignments which silently narrow a value, such asint i; i += 1.5;.
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.Continue Reading Java 18 to 21 Tutorials
Overview of New Features in Java 21Click to read tutorial: Overview of New Features in Java 21 Overview of New Features in Java 19Click to read tutorial: Overview of New Features in Java 19 Overview of New Features in Java 18Click to read tutorial: Overview of New Features in Java 18 Java 18 UTF-8 by Default: What Changes and WhyClick to read tutorial: Java 18 UTF-8 by Default: What Changes and Why Java 18 Simple Web Server (jwebserver) TutorialClick to read tutorial: Java 18 Simple Web Server (jwebserver) Tutorial Java 18 Code Snippets in Javadoc with @snippetClick to read tutorial: Java 18 Code Snippets in Javadoc with @snippet Java 18 Deprecates Finalization: Use Cleaner InsteadClick to read tutorial: Java 18 Deprecates Finalization: Use Cleaner Instead Java 21 Virtual Threads Tutorial with ExamplesClick to read tutorial: Java 21 Virtual Threads Tutorial with Examples Java 21 Record Patterns with ExamplesClick to read tutorial: Java 21 Record Patterns with Examples
Overview of New Features in Java 21Click to read tutorial: Overview of New Features in Java 21 Overview of New Features in Java 19Click to read tutorial: Overview of New Features in Java 19 Overview of New Features in Java 18Click to read tutorial: Overview of New Features in Java 18 Java 18 UTF-8 by Default: What Changes and WhyClick to read tutorial: Java 18 UTF-8 by Default: What Changes and Why Java 18 Simple Web Server (jwebserver) TutorialClick to read tutorial: Java 18 Simple Web Server (jwebserver) Tutorial Java 18 Code Snippets in Javadoc with @snippetClick to read tutorial: Java 18 Code Snippets in Javadoc with @snippet Java 18 Deprecates Finalization: Use Cleaner InsteadClick to read tutorial: Java 18 Deprecates Finalization: Use Cleaner Instead Java 21 Virtual Threads Tutorial with ExamplesClick to read tutorial: Java 21 Virtual Threads Tutorial with Examples Java 21 Record Patterns with ExamplesClick to read tutorial: Java 21 Record Patterns with Examples