Java 20 was officially released with the introduction of its general availability on March 21, 2023. This is the 10th release of the language, that followed the six-month release window that has become standard. That is why we know the next release, Java 21, is expected to be available in September 2023.
While Java 19 introduced groundbreaking virtual threads, Java 20 is a smaller version in terms of new features. The main new feature in Java 20 is 'Scoped Values', which is intended to replace thread-local variables. In addition to this, it contains a total of six JDK Enhancement Proposals called JEPs, all of which are incubator or preview features. This means that these features are still in development and must be explicitly enabled via the '--enable-preview' option in the java and javac commands to be tested. The six JEPs that come with this release are divided into three groups, which correspond to Project Amber (aimed at increasing developer productivity), Project Loom (aimed at reimagining threading), and Project Panama (aimed at combining Java with other platforms). In addition to these categories, there is also another JEP that focuses on vector math.
Record patterns (JEP 432)
In its second preview, record patterns introduce a new set of patterns to deconstruct record values, enhancing the Java programming language.
Record patterns and type patterns can be nested, allowing for declarative and powerful data navigation and processing. The primary objective is to extend pattern matching capabilities to express more sophisticated and composable data queries without changing the syntax or semantics of type patterns. The significant changes made since the first preview in JDK 19 include adding support for inference of type arguments of generic record patterns, enabling record patterns to appear in the header of an enhanced for statement, and the removal of support for named record patterns.
Pattern matching for switch (JEP 433)
Pattern matching for switch statements and expressions provides a concise and secure method for expressing complex data-oriented queries. This feature was previously previewed in JDK 17, JDK 18, and JDK 19. In the fourth preview, it continues to evolve alongside Record Patterns, allowing for further refinements based on feedback and experience.
The key changes in pattern matching for switch statements since the third preview include simplified grammar for switch labels and support for type argument inference for generic patterns and record patterns in switch statements and expressions. Additionally, an exhaustive switch over an enum class now throws a MatchException instead of an IncompatibleClassChangeError if no switch label applies at runtime.
Scoped values (JEP 429)
Currently, in the incubation stage of development, the Scoped Values API facilitates the sharing of immutable data within and between threads. This approach is preferred over thread-local variables, especially in situations with a high number of virtual threads. With scoped values, data can be safely and efficiently shared among various components in a large program without relying on method arguments. The primary objectives of this API are to provide ease of use, comprehensibility, robustness, and high performance.
Virtual threads (JEP 436)
In its second preview, virtual threads are designed as lightweight threads that significantly reduce the effort required for writing, maintaining, and observing high-throughput concurrent applications. According to Oracle, this feature will fundamentally transform the way Java applications are scaled. Minor changes have been made since the first preview in JDK 19, including a few permanent API changes in JDK 19 and downgrades to ThreadGroup, also made permanent in JDK 19.
Structured concurrency (JEP 437)
Structured concurrency provides an API to simplify multithreaded programming by treating multiple tasks running in different threads as a single unit of work. This approach streamlines error-handling and cancellation, enhancing both reliability and observability. The only modification since the feature was incubated in JDK 19 is the update to StructuredTaskScope, which now supports the inheritance of scoped values by threads created in a task scope. This feature is being re-incubated.
Foreign function and memory API (JEP 434)
The Foreign Function and Memory API introduces an interface that allows Java programs to interact with code and data outside of the Java runtime. With this API, Java programs can call native libraries and process native data without the risks and limitations of JNI (Java Native Interface). The API was previously under incubation in JDK versions 17 and 18 and previewed in JDK 19. Refinements have been made since then, such as the unification of MemorySegment and MemoryAddress abstractions, an improved MemoryLayout hierarchy, and the division of MemorySession into Arena and SegmentScope, which enables the sharing of segments across maintenance boundaries.
One more JEP
Vector API (JEP 438)
Recently, the vector API proposal was officially added, providing an API to express vector computations that can compile at runtime as optimal vector instructions on supported CPU architectures. By utilizing this API, superior performance can be achieved compared to equivalent scalar computations. The vector API has been incubated in JDK 16, JDK 17, JDK 18, and JDK 19.
Deprecation / Deletions
- java.net.URL constructors are deprecated
- Thread.suspend/resume changed to throw UnsupportedOperationException
Smaller changes in Java 20
- Javac Warns about Type Casts in Compound Assignments with Possible Lossy Conversions
- Idle Connection Timeouts for HTTP/2
- HttpClient Default Keep Alive Time is 30 Seconds
- IdentityHashMap's Remove and Replace Methods Use Object Identity
- The new method Path::getExtension returns the file’s extension
- Datagram Transport Layer Security (DTLS) key exchange named groups
- The addition of a new GarbageCollectorMXBean to report the occurrence and duration of remark and cleanup pause times for the G1 garbage collector
- ChaCha20 intrinsics on x86_64 and aarch64 platforms
- Support for Unicode 15.0
- Improved control of G1’s concurrent refinement threads
- Support for Unicode CLDR version 42
- New JDK Flight Recorder events: jdk.InitialSecurityProperty and jdk.SecurityProviderService
- Improvements to the Preview API page in the documentation generated by Javadoc
- New jmod –compress command-line option
- Grapheme support in Break Iterator
- Updated time zone data that complies with the 2022c standard
- Auto Generated IDs in Javadoc headings
The main benefit of the latest version of Java is the introduction of 'Scoped Values', which provides a beneficial tool for assigning a read-only, thread-specific value to a thread or group of child threads for their lifetime.
Other changes in Java 20 are mostly slightly modified and improved versions of already introduced JEPs, as well as minor improvements, routine maintenance, and bug fixes.
While the changes introduced in the updated version of Java may not seem groundbreaking, they do offer some significant improvements to the platform. Moreover, these changes provide an insight into the evolution of the Java language and give us some idea of what to expect from Java 21, which is likely to be a more groundbreaking release as it will carry the LTS tag that Oracle will support for the next five years.
Even though this release will not phase out Java 8’s adoption, each release brings us closer to the date it will inevitably happen.