What are the new features of Java-20?

In March 2023, Oracle released a new version of Java as Java-20. Following are the new features of Java-20.

Scoped Values (Incubator)

  • This enables the sharing of immutable data within and across threads.
  • They are preferred to thread-local variables, especially when using large numbers of virtual threads.
  • This is an incubating API.

Record Patterns (Second Preview)

  • This is added as a preview feature as part of the Java-19 update. In Java-20 new enhancements are added. Java-20 also has this as a preview feature.
  • Java-20  added support for inference of type arguments of generic record patterns,
  • Also added support for record patterns to appear in the header of an enhanced statement
  • This release removed support for named record patterns.

Pattern Matching for switch (Fourth Preview)

  • We have already seen the pattern matching for switch in java-17, 18, and 19.
  • This Java version proposes a fourth preview to enable its continued co-evolution with the Record Patterns preview feature and to allow other refinements based on continued experience and feedback
  • The main changes since the third preview are:
  • Switch expression or a pattern switch statement over an enum class now throws MatchException rather than IncompatibleClassChangeError if no switch label applies at run time.
  • The grammar for switch labels is simpler.
  • Inference of type arguments for generic record patterns is now supported in switch expressions and statements, along with the other constructs that support patterns.

Foreign Function & Memory API (Second Preview)

  • This API was incubated in Java-17 and re-incubated in Java-18.
  • We have seen the first preview of this API in Java-19.
  • This version proposes to incorporate refinements based on feedback and to re-preview the API in JDK 20. In this version:
    • The MemorySegment and MemoryAddress abstractions are unified.
    • The sealed MemoryLayout hierarchy is enhanced to facilitate usage with pattern matching in switch expressions and statements (JEP 433), and
    • MemorySession has been split into Arena and SegmentScope to facilitate sharing segments across maintenance boundaries.

Virtual Threads (Second Preview)

  • This was introduced as a preview feature in Java-19. 
  • This version proposes a second preview to allow time for more feedback and to get more experience with this feature.
  • Minor changes done for this release are as follows.
  • Some of the APIs introduced in Java-19 are made permanent because they involve functionality that is broadly useful and is not specific to virtual threads.
  • They consist of new methods in Thread (join(Duration), sleep(Duration), and threadId()), new methods in Future (to examine task state and result), and the change to make ExecutorService extend AutoCloseable.

Structured Concurrency (Second Incubator)

  • Simplify multithreaded programming by introducing an API for structured concurrency. 
  • Structured concurrency treats multiple tasks running in different threads as a single unit of work, thereby streamlining error handling and cancellation, improving reliability, and enhancing observability. 
  • This is an incubating API.
  • Structured Concurrency was introduced in JDK 19 as an incubating API.
  • This version proposes to re-incubate the API, without changes, in Java 20 to allow time for more feedback and to get more experience with this feature.
  • The only change in the re-incubated API is that StructuredTaskScope is updated to support the inheritance of scoped values by threads created in a task scope. This streamlines the sharing of immutable data across threads.

Vector API (Fifth Incubator)

  • This was introduced in Java-16, 17, 18 and 19. This release also proposes to re-incubate the API in Java-20, with no changes in the API relative to Java-19. 
  • The implementation includes a small set of bug fixes and performance enhancements. 
  • This version also clarifies that alignment with Project Valhalla is a critical part of completing the Vector API.

Also, see:

-A blog by Shwetali Khambe

Related Posts