JDK 20: The new features in Java 20

Java / coffee / beans

Java Development Kit (JDK) 20, an upgrade of standard Java due later this month, is set to add a seventh feature—a fifth incubation of a vector API—despite the feature set previously being frozen at six capabilities.

The vector API proposal would introduce an API to express vector computations that reliably compile at runtime as optimal vector instructions on supported CPU architectures. This would achieve performance superior to equivalent scalar computations. The vector API previously has been incubated in JDK 16, JDK 17, JDK 18, and JDK 19. A review period for the vector API plan is ongoing until March 8. JDK 20, now in a release candidate phase, is due for general availability March 21.

The other six features officially marked for the release also are either in an incubation or in a preview stage. These include scoped values, record patterns, pattern matching for switch statements and expressions, a foreign function and memory API, virtual threads, and structured concurrency.

JDK 20 follows the September 20 release of JDK 19. Early-access builds of JDK 20 can be found at jdk.java.net.

These are the specifics on the other six features slated for JDK 20:

  • Scoped values, an API in an incubator stage of development, enables the sharing of immutable data within and across threads. These are preferred to thread-local variables, particularly when using large numbers of virtual threads. A scoped value allows data to be shared safely and efficiently between components in a large program without resorting to method arguments. Goals include ease of use, comprehensibility, robustness, and performance.
  • Record patterns, in a second preview, enhance the Java programming language with patterns to deconstruct record values. Record patterns and type patterns can be nested to enable a declarative, powerful, and composable form of data navigation and processing. Goals include extending pattern matching to express more sophisticated, composable data queries and not changing the syntax or semantics of type patterns. Main changes since the first preview in JDK 19 include adding support for inference of type arguments of generic record patterns, support for record patterns to appear in the header of an enhanced for statement, and removing support for named record patterns.
  • Foreign function and memory API introduces an API by which Java programs can interoperate with code and data outside of the Java runtime. The API enables Java programs to call native libraries and process native data without the brittleness and danger of JNI (Java Native Interface). The API was previously incubated in JDK Versions 17 and 18 and previewed in JDK 19. Refinements added since JDK 19 include unification of MemorySegment and MemoryAddress abstractions, an enhanced MemoryLayout hierarchy, and splitting of MemorySession into Arena and SegmentScope to promote sharing of segments across maintenance boundaries.
  • Virtual threads, in a second preview, are lightweight threads that reduce the effort of writing, maintaining, and observing high-throughput concurrent applications. Minor changes since the first preview in JDK 19 include a small number of API changes (made permanent in JDK 19) and degradations to ThreadGroup (also made permanent in JDK 19).
  • Structured concurrency features an API to simplify multithreaded programming and treats multiple tasks running in different threads as a single unit of work. Error-handling and cancellation are streamlined, improving reliability and enhancing observability. The only change since being incubated in JDK 19 is that StructuredTaskScope has been updated to support inheritance of scoped values by threads created in a task scope. This feature is being reincubated.
  • Pattern matching for switch statements and expressions enables the concise and safe expression of complex data-oriented queries. Previously previewed in JDK 17, JDK 18, and JDK 19, this fourth preview would enable a continued co-evolution with Record Patterns, allowing for continued refinements based on experience and feedback. The main changes in pattern matching for switch since the third preview include simplified grammar for switch labels and support for inference of type arguments for generic patterns and record patterns in switch statements and expressions. Also, an exhaustive switch over an enum class now throws MatchException rather than IncompatibleClassChangeError if no switch label applies at runtime.

Capabilities that did not make it into JDK 20 but are still under consideration for Java include universal generics, string templates, sequenced collections, and an asynchronous stack trace VM API. String templates and the asynchronous stack trace VM API now are slotted for JDK 21, due in September 2023.

JDK 20 is set to be a short-term feature release, with only six months of Premier-level support from Oracle. JDK 21 will be a long-term support release, backed by multiple years of support.

Copyright © 2023 IDG Communications, Inc.


Source link