In case you are looking forward to the next LTS Java release and forgot about minor JDK versions — JDK 20 reminds of its existence! It has entered the Rampdown Phase Two, and the feature set has been frozen. Let’s summarize the JEPs targeted for JDK 20 to brush up on where Java is going.
JEP 429: Scoped Values (Incubator)
JEP 429: Scoped values is the only new feature introduced in JDK 20 as an Incubator API. Its goal is to facilitate thread management by sharing immutable data within and across threads. Scoped values prohibit the change of a variable by remote code, so the data can be reliably communicated to callees in the same method.
Scoped values will be a great asset for developers working with virtual threads, the number of which can reach thousands or even millions. You can find more details on the JEP in a dedicated short article.
JEP 432: Record Patterns (Second Preview)
Record patterns were introduced in JDK 19 as a Preview feature. Record patterns are used for record values deconstruction. They promote more declarative programming and facilitate code writing because they are nestable and can be matched against and decomposed by a nested pattern.
JEP 432 proposes a Second Preview with several enhancements:
- Support for inference of type arguments of generic record patterns;
- Support for record patterns to appear in the header of an enhanced for statement;
- Removed support for named record patterns.
JEP 433: Pattern Matching for switch (Fourth Preview)
JEP 433 introduces a Forth Preview of pattern matching for switch expressions and statements. The improvements include:
- The simplification of the grammar for switch labels;
- Changed type of an exception thrown when no switch label applies at run time. A switch statement or expression over an enum class throws MatchException instead of IncompatibleClassChangeError;
- Support for inference of type arguments for generic record patterns in switch statements or expressions.
JEP 434: Foreign Function & Memory API (Second Preview)
JEP 434 brings to JDK 20 a Second Preview of Foreign Function & Memory (FFM) API, first introduced in JDK 17 as Incubator API. FFM is more safe by design than Java Native Interface and provides a more robust way of working with foreign code and memory not managed by the JVM. JEP 434 includes the following enhancements:
- Unified MemorySegment and MemoryAddress abstractions;
- Improved sealed MemoryLayout hierarchy enables convenient usage of FFM with pattern matching in switch expressions and statements;
- Split MemorySession into Arena and SegmentScope facilitates sharing segments across maintenance boundaries.
JEP 436: Virtual Threads (Second Preview)
Virtual threads will bring multithreaded programming in Java to a completely different level. In short, instead of using few OS threads or diving into asynchronous programming, developers can have thousands or millions of virtual threads, which are not tied to an OS thread for the whole lifetime of the code. At the same time, they are easy to debug, monitor, or profile. To find out more about this exciting feature, read a short article dedicated to the topic.
JEP 436: Virtual Threads is a Second Preview with minor changes to functionality not specific to virtual threads only.
JEP 437: Structured Concurrency (Second Incubator)
Structured concurrency will improve reliability and maintainability of multithreaded Java code and helps to coordinate thousands of virtual threads efficiently. With the new library for structured concurrency, developers form a tree-shaped hierarchy of tasks owned by a parent task. This limits the lifetime of subtasks within a single syntactic block and helps to prevent thread leaks or any other unexpected behavior of multithreaded code.
JEP 437 updates the StructuredTaskScope to support the inheritance of scoped values by threads created in a task scope, thus streamlining the sharing of immutable data across threads.
JDK 20 goes GA in March 2023. Early-access builds are available to experiment with.
The next JDK 21 will enjoy long-term support and is fit for enterprise use. So if you are planning the migration to Java 21 LTS, use JDK 20 as a shooting range to test the new functionality and see how well it goes with your current application structure!