Skip to content
Sebastian Fischer edited this page Dec 20, 2023 · 9 revisions

JDK 21 is a long-term support release of Java that changes how you can write programs. Project Amber has delivered pattern matching features that make the switch construct significantly more expressive, so using custom data types gets much less verbose. Project Loom has delivered a new implementation of threads that allows asynchronous programs to be written in a synchronous style, so traditional concurrent programs become more scalable. Additionally, the Collections Framework is extended with interfaces for collections that have a defined order of elements, unifying existing operations to access elements of a sequenced collection at both ends.

There also is a preview feature supporting embedded expressions in literal text in a way that systematically avoids common pitfalls with string interpolation. JDK 21 has additional preview features related to pattern matching, concurrency, and more, but we leave those to be discussed elsewhere.

This demo uses example programs to show how the mentioned features enable new ways to write programs. It contains tasks to extend those programs so you can run experiments yourself. You can clone this repository and open it in your favorite Java IDE, or use a predefined development environment with VSCode Dev Containers. If you are using Maven (or the predefined environment) you can run mvn test to check if your environment is setup properly.

This Demo has four parts.

  • Part 1 introduces new features for Pattern Matching in Java, simplifying the handling of algebraic data types defined using sealed classes and records. It demonstrates practical applications in error handling and data traversal with use cases like file search and transformations of tree-structured data. Pattern matching can make programs safer with compile-time checks for exhaustiveness and enables functional programming patterns that allow for more compositional program designs.
  • Part 2 introduces Virtual Threads, a new lightweight thread implementation, focused on high-throughput concurrency. It demonstrates the advantage of virtual threads in scenarios with blocking operations, e.g. for network communication, comparing different threading models like pooling and creating a new thread for each new task. It also introduces tools to monitor the resource usage of Java programs.
  • Part 3 introduces Sequenced Collections, new interfaces in the Collections Framework for collections with a defined order of elements, unifying and extending the functionality of existing collection types. It demonstrates the extension of a server application with sequenced collections, providing insights into real-world applications and considerations for thread safety and collection characteristics.
  • Part 4 introduces String Templates, a new syntax and API for safe and extensible string interpolation in Java. It demonstrates how to use predefined template processors and define new ones for custom data types.
Clone this wiki locally