Skip to content
Sebastian Fischer edited this page Nov 17, 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 your own experiments. 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.

In the first part about Pattern Matching we define an immutable data type for arithmetic expressions together with operations to transform them. After discussing the new pattern matching features with a simple example we look at more advanced programming patterns allowing to implement complex recursive operations in a compositional style. We will also see different examples of how error handling can be improved for programs written in functional style, by using a custom data type extending the idea of the Optional API.

In the second part about Virtual Threads we will implement a simple server application and compare its performance when using different underlying thread implementations. We will also extend the implementation with a specific kind of logging to try out the new interfaces for Sequenced Collections.

In the final part about String Templates we will look at a preview of how string interpolation will work in a future version of Java and implement a custom template processor for arithmetic expressions.

Clone this wiki locally