What are the new features of java-19?

On September 2022, Oracle released a new version of Java as Java-19. Following are the new features of java-19.

Record Patterns (Preview)

  • In Java-16, 17, and 18 we have seen what is pattern matching.
  • Java-19 extends pattern matching to express more sophisticated, composable data queries.
  • Record classes are transparent carriers for data. Code that receives an instance of a record class will typically extract the data, known as the components. 
  • For example, we can use a type pattern to test whether a value is an instance of the record class Sum and, if so, extract the a and b from the object:

In the above example, we can check if the object is an instance of Sum as well as extract the values of a and b at the same time. The code will be updated below.

Linux/RISC-V Port

  • The port will support the following HotSpot subsystems:
    • The template interpreter,
    • The C1 (client) JIT compiler,
    • The C2 (server) JIT compiler, and
    • All current mainline GCs, including ZGC and Shenandoah.

Foreign Function & Memory API (Preview)

  • We have seen this feature introduced as a preview feature in java-17 and java-18. 
  • In Java-19 as well this is added as a preview feature with incorporates refinements to the FFM API based on previous release feedback.

Virtual Threads (Preview)

  • Java-19 introduces virtual threads to the Java Platform. 
  • Virtual threads are lightweight threads that dramatically reduce the effort of writing, maintaining, and observing high-throughput concurrent applications. 
  • Note that this is a preview feature in java-19.
  • Server applications can be written in the simple thread-per-request style to scale with near-optimal hardware utilization.
  • This enables existing code that uses the java.lang.Thread API to adopt virtual threads with minimal change.
  • It becomes easy to troubleshoot, debugging, and profiling of virtual threads with existing JDK tools.
  • Following is an example of a virtual thread program that first obtains an ExecutorService that will create a new virtual thread for each submitted task. It then submits 10,000 tasks and waits for all of them to complete:
  • The task in this example is simple code sleep for one second and modern hardware can easily support 10,000 virtual threads running such code concurrently. 
  • Behind the scenes, the JDK runs the code on a small number of OS threads, perhaps as few as one.
  • If we are using ExecutorService then it will create a new platform thread for each task, such as Executors.newCachedThreadPool(). The ExecutorService would attempt to create 10,000 platform threads, and thus 10,000 OS threads and the program might crash, depending on the machine and operating system.

Vector API (Fourth Incubator)

  • Vector API is first introduced as a preview feature in java-16 and then followed by java-17, java-18, and java 19.
  • In java-19 some enhancements in response to feedback as well as performance improvements and other significant implementation enhancements are added.
  • Enhance the API to load and store vectors to and from MemorySegments as defined by Foreign Function & Memory (FFM) API (Preview). 
  • Added two new cross-lane vector operations, compress and its inverse expand, together with a complementary vector mask compress operation.
  • Expand the supported set of bitwise integral lane-wise operations

Pattern Matching for switch (Third Preview)

  • This is added as a preview feature in java-17 and java-18. This release has a third preview with further refinements based on continued experience and feedback.
  • Following is an example of the switch with pattern matching.
  • Traditionally, switch statements and expressions throw NullPointerException if any object is null, so we needed to add a null check before the switch case, but now we can also add a null case if any object is null as shown in the below example.

Structured Concurrency (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.
  • This enhances the maintainability, reliability, and observability of multithreaded code by adopting a concurrent programming style that reduces the likelihood of thread leaks and cancellation delays, which are common risks associated with cancellation and shutdown.
  • This feature is added to
    • Improve the maintainability, reliability, and observability of multithreaded code.
    • Promote a style of concurrent programming that can eliminate common risks arising from cancellation and shut down, such as thread leaks and cancellation delays

Also, see:

-A blog by Shwetali Khambe

Related Posts