Overview of New Features in Java 19

Introduction Java 19 was released on 20th September 2022, with seven JDK Enhancement Proposals (JEPs). Six of them were previews or incubators - features which are complete enough to try, but not yet final. That makes Java 19 look small on paper, but historically it is important: it was the first release with virtual threads, structured concurrency and record patterns, the features which, together with pattern matching for switch, made Java 21Click to read tutorial: Overview of New Features in Java 21 such a significant release. This article gives an overview of the new features in Java 19, including the small library additions which were final, with links to the detailed tutorials of the final versions. Preview and incubator features A preview language or API feature must be enabled with --enable-preview when compiling and running; an incubator API lives in a jdk.incubator.* module which must be added with --add-modules. Both can change before they are finalized.
  1. Virtual Threads (Preview) - JEP 425 - lightweight threads managed by the JVM, which make simple blocking code scale to millions of concurrent tasks. Final in Java 21 - read the virtual threads tutorialClick to read tutorial: Java 21 Virtual Threads Tutorial with Examples.
  2. Structured Concurrency (Incubator) - JEP 428 - an API which treats a group of related tasks running in different threads as a single unit of work, so that errors and cancellation are handled together. It was still in preview in Java 25.
  3. Record Patterns (Preview) - JEP 405 - take records apart into their components in instanceof and switch. Final in Java 21 - read about record patternsClick to read tutorial: Java 21 Record Patterns with Examples.
  4. Pattern Matching for switch (Third Preview) - JEP 427 - final in Java 21 - read about pattern matching for switchClick to read tutorial: Java 21 Pattern Matching for switch with Examples.
  5. Foreign Function & Memory API (Preview) - JEP 424 - moved from incubator to preview; final in Java 22 - read the Foreign Function & Memory API tutorialClick to read: Foreign Function & Memory API tutorial.
  6. Vector API (Fourth Incubator) - JEP 426 - continued incubation of the SIMD Vector API.
Platform
  1. Linux/RISC-V Port - JEP 422 - the JDK was ported to Linux on the RISC-V processor architecture, an open instruction set used increasingly in embedded and server hardware.
Final library additions Several small, final API additions in Java 19 are useful in everyday code -
  • HashMap.newHashMap(n), HashSet.newHashSet(n), LinkedHashMap.newLinkedHashMap(n) and LinkedHashSet.newLinkedHashSet(n) create collections sized to hold n elements without resizing. The old new HashMap<>(n) takes a capacity, not a number of elements, so it resized once about three-quarters full - a common mistake these methods fix.
  • Locale.of(language, country) replaces the now-deprecated Locale constructors.
  • Future got resultNow(), exceptionNow() and state(), for reading a completed task's result without checked exceptions.
  • ExecutorService now implements AutoCloseable: close() waits for submitted tasks to finish, so an executor can be used in a try-with-resources block - used heavily with virtual threadsClick to read tutorial: Java 21 Virtual Threads Tutorial with Examples.
  • Thread.threadId() replaces the deprecated Thread.getId(), and Integer.compress() and expand() (with Long versions) add fast bit-manipulation operations.
  • The system properties stdout.encoding and stderr.encoding report the encoding used by System.out and System.err, complementing UTF-8 by defaultClick to read tutorial: Java 18 UTF-8 by Default: What Changes and Why.
Summary In this article we went through the new features of Java 19. Almost all of them were previews and incubators - virtual threads, structured concurrency, record patterns, pattern matching for switch, the Foreign Function & Memory API and the Vector API - which became the core of Java 21 and 22. Java 19 also added the Linux/RISC-V port, and small final library additions such as HashMap.newHashMap(), Locale.of(), Future.resultNow() and a closeable ExecutorService. Next, read the overview of new features in Java 20Click to read tutorial: Overview of New Features in Java 20.