Javatpoint Logo
Javatpoint Logo

String Templates in Java 21

Java 21, the latest release of the Java programming language, brings several exciting features and enhancements to the table. One of these notable features is the introduction of string templates, which simplify string formatting and interpolation. In this section, we'll delve into the world of string templates in Java 21 and learn how they can make code more concise and readable.

String Concatenation

Before we dive into string templates, let's briefly discuss the traditional method of string concatenation in Java. In previous versions of the language, combining strings involved using the + operator or the String.concat() method. Here's a quick example:

While this approach works, it can become unwieldy and error-prone when dealing with complex string compositions. Moreover, it lacks readability and can clutter the code, especially when mixing variables and literal strings.

String Templates

String templates in Java 21 provide a more elegant and efficient way to format strings. It allows us to embed expressions directly within string literals, making code cleaner and more expressive. Let's take a look at how string templates work.

Basic String Templates

To create a basic string template in Java 21, you enclose expressions within ${} placeholders within a string literal. Here's an example:

In this example, ${firstName} and ${lastName} are placeholders that will be replaced with the values of the corresponding variables. The resulting fullName string will be "John Doe".

Expressions and Functions

String templates support more than just variable interpolation. We can also include expressions and even call functions within the placeholders. For instance:

In this example, the placeholders ${x} and ${y} are replaced with the values of x and y, while ${x + y} evaluates the expression and substitutes the result into the string. The resulting result string will be "The sum of 5 and 7 is 12".

Escaping and Literal ${}

If we need to include ${} literals in string without interpolation, we can escape them using a double dollar sign ($$). For example:

It will result in the string "This is a literal ${expression} without interpolation".

Benefits of String Templates

String templates offer several advantages over traditional string concatenation:

  • Readability: String templates make your code more readable by clearly separating variables, expressions, and literals within the string.
  • Conciseness: You can express complex string compositions more concisely and with fewer lines of code.
  • Maintainability: When you need to modify a formatted string, string templates make it easier to update the content without introducing errors.
  • Performance: String templates can be more efficient than concatenation, especially for large strings, as they allow for more efficient memory management.

Here are some example code snippets demonstrating the use of string templates in Java 21:

Basic String Templates

These examples demonstrate how string templates in Java 21 allows us to interpolate variables, expressions, and even escape ${} literals within strings, making string formatting more concise and readable.


String templates are a welcome addition to Java 21, offering a more elegant and efficient way to format strings. They improve code readability, make string compositions more concise, and enhance maintainability. By embracing string templates, Java developers can write cleaner and more expressive code, ultimately leading to better software development practices.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA