Java 21 features with code examples

FullStackTips
5 min readSep 13, 2023
Java 21 key features

Java 21 is the latest long-term support (LTS) release of the Java Development Kit (JDK). As per the release schedule, JDK 21 will be formally released on September 19, 2023. It introduces a number of new features and enhancements that aim to make Java more expressive, concise, and performant. In this blog story, we will look at some of the key features of Java 21 and explore how they can be used to improve the development experience.

String Templates

String templates are a new feature in Java 21 that allows you to embed expressions in string literals. This makes it easier to format strings and generate dynamic content.

For example, the following code uses a string template to generate a greeting message.

var name = "Kenneth";
var greeting = STR."Hello \{name}, how are you?\nCan you give me a call?";

Here STR is the template processor and it is automagically imported into every Java source file.

String templates are a convenient way to generate formatted strings. They are also concise and easy to read.

Pattern matching for switch expressions and statements.

This feature allows you to deconstruct values and perform different actions based on the results.

--

--

FullStackTips

I am full stack developer with over 15 years of experience in various programming languages. https://medium.com/@fullstacktips/membership