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
Platform
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
--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.
- 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.
- 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.
- Record Patterns (Preview) - JEP 405 - take records apart into their components in
instanceofandswitch. Final in Java 21 - read about record patternsClick to read tutorial: Java 21 Record Patterns with Examples. - 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.
- 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.
- Vector API (Fourth Incubator) - JEP 426 - continued incubation of the SIMD Vector API.
- 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.
HashMap.newHashMap(n),HashSet.newHashSet(n),LinkedHashMap.newLinkedHashMap(n)andLinkedHashSet.newLinkedHashSet(n)create collections sized to hold n elements without resizing. The oldnew 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-deprecatedLocaleconstructors.FuturegotresultNow(),exceptionNow()andstate(), for reading a completed task's result without checked exceptions.ExecutorServicenow implementsAutoCloseable: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 deprecatedThread.getId(), andInteger.compress()andexpand()(withLongversions) add fast bit-manipulation operations.- The system properties
stdout.encodingandstderr.encodingreport the encoding used bySystem.outandSystem.err, complementing UTF-8 by defaultClick to read tutorial: Java 18 UTF-8 by Default: What Changes and Why.
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.Continue Reading Java 18 to 21 Tutorials
Overview of New Features in Java 20Click to read tutorial: Overview of New Features in Java 20 Java 18 Deprecates Finalization: Use Cleaner InsteadClick to read tutorial: Java 18 Deprecates Finalization: Use Cleaner Instead Overview of New Features in Java 18Click to read tutorial: Overview of New Features in Java 18 Overview of New Features in Java 21Click to read tutorial: Overview of New Features in Java 21 Java 18 UTF-8 by Default: What Changes and WhyClick to read tutorial: Java 18 UTF-8 by Default: What Changes and Why Java 18 Simple Web Server (jwebserver) TutorialClick to read tutorial: Java 18 Simple Web Server (jwebserver) Tutorial Java 18 Code Snippets in Javadoc with @snippetClick to read tutorial: Java 18 Code Snippets in Javadoc with @snippet Java 21 Virtual Threads Tutorial with ExamplesClick to read tutorial: Java 21 Virtual Threads Tutorial with Examples Java 21 Record Patterns with ExamplesClick to read tutorial: Java 21 Record Patterns with Examples
Overview of New Features in Java 20Click to read tutorial: Overview of New Features in Java 20 Java 18 Deprecates Finalization: Use Cleaner InsteadClick to read tutorial: Java 18 Deprecates Finalization: Use Cleaner Instead Overview of New Features in Java 18Click to read tutorial: Overview of New Features in Java 18 Overview of New Features in Java 21Click to read tutorial: Overview of New Features in Java 21 Java 18 UTF-8 by Default: What Changes and WhyClick to read tutorial: Java 18 UTF-8 by Default: What Changes and Why Java 18 Simple Web Server (jwebserver) TutorialClick to read tutorial: Java 18 Simple Web Server (jwebserver) Tutorial Java 18 Code Snippets in Javadoc with @snippetClick to read tutorial: Java 18 Code Snippets in Javadoc with @snippet Java 21 Virtual Threads Tutorial with ExamplesClick to read tutorial: Java 21 Virtual Threads Tutorial with Examples Java 21 Record Patterns with ExamplesClick to read tutorial: Java 21 Record Patterns with Examples