Changes in Java 19

Java 19 was released in September 2022, and it is currently the latest version. The last LTS version is Java 17. Here we will list some of the important changes:

Changes to Preallocated HashMap Creation

When creating HashMaps, you would usually write something like this:

Map<> map = new HashMap<>(100)

The issue is that HashMaps are initialized with load factor 75, so as soon there are more than 75 entries, map will double in size. In order to have map with 100 hundred we would need to initialize map with 133. Instead of that we can write this:

Map<> map = HashMap.newHashMap(100);

JEP 427: Pattern Matching for Switch (Third preview)

Improves Java development productivity by expanding the expressiveness and applicability of switch expressions and statements. Allowing pattern matching in switch will make expressing complex data-oriented queries more concise and safer.

The enhancements in this preview changed the syntax of the so-called “Guarded Pattern”. Instead of:

case String s && s.length() > 5 ->System.out.println(s.toUpperCase());

We can use

case String s when s.length()>5 ->System.out.println(s.toUpperCase());

So instead && we use when.


JEP 405: Record Patterns Preview

Enhances the Java programming language with record patterns to deconstruct record values. Record patterns and type patterns can be nested to enable a powerful, declarative, and composable form of data navigation and processing.

Instead of:

switch (object) {
    case Position position
        -> System.out.println("x = " + position.x() + “y=” + position.y()) 


We can write:

switch (object) {
    case Position position(int x, int y)
        -> System.out.println("x = " + x + “y = ” + y)

Similar to that, we can use instanceof:
if(object instanceof Position(int x, int y)){
    System.out.println("x = " + x + “y = ” + y)

This we can access x and y directly. This can also be done with nested records.     

JEP 424: Foreign Function & Memory API (Preview)

The Foreign Function & Memory API introduces an API by which Java programs can interoperate with code and data outside of the Java runtime. By efficiently invoking foreign functions (i.e., code outside the JVM), and by safely accessing foreign memory (i.e., memory not managed by the JVM), the API enables Java programs to call native libraries and process native data without the brittleness and danger of JNI.

JEP 428: 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, thereby streamlining error handling and cancellation, improving reliability, and enhancing observability. 

JEP 425: Virtual Threads (Preview)

Introduce 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.

JEP 426: Vector API (Fourth Incubator)

Introduces a platform-agnostic, reliable way of clearly and concisely expressing a wide range of vector computations for supported CPU architectures. 

JEP 422: Linux/RISC-V Port

A port to Linux on the RISC-V chip architecture was developed. RISC-V is a free and open-source RISC instruction set architecture (ISA) designed originally at the University of California, Berkeley There are not many actual RISC-V devices you can buy right now, but this is a promising-looking platform, so Java support will be beneficial.