Harnessing the Power of String Templates: A Guide for Java Developers

George Adams
3 min readApr 14, 2023

--

String templates, set to be introduced as a preview feature in JDK 21, simplify string formatting and manipulation in Java. This feature allows developers to embed expressions directly within string literals, making it easier to build and format complex strings. In this blog post, we will explore string templates and provide practical examples to help Java developers get started with this powerful new feature.

Understanding String Templates

String templates, also known as “interpolated strings”, enable developers to embed expressions within string literals. This eliminates the need for cumbersome concatenation and formatting methods like String.format(). With string templates, you can write more concise and readable code that is easier to maintain.

Using String Templates for Simple Expressions

To get started with string templates, let’s look at a basic example. Suppose you want to create a greeting message that includes the user’s name:

String userName = "Alice";
String greeting = "Hello, " + userName + "!";
System.out.println(greeting); // Output: Hello, Alice!

With string templates, you can simplify this code as follows:

String userName = "Alice";
String greeting = `Hello, ${userName}!`;
System.out.println(greeting); // Output: Hello, Alice!

In this example, we embed the userName variable directly within the string literal using the ${expression} syntax.

Using String Templates with Complex Expressions

String templates can also handle more complex expressions, such as method calls or arithmetic operations. Here’s an example that calculates the total price of a purchase, including tax:

double price = 100.0;
double taxRate = 0.2;
double totalPrice = price * (1 + taxRate);
String message = `Total price (including tax): ${totalPrice} GBP`;
System.out.println(message); // Output: Total price (including tax): 120.0 GBP

In this example, the string template calculates the total price by embedding the totalPrice variable directly within the string literal.

Formatting Strings with String Templates

You can control the formatting of embedded expressions in string templates using format specifiers. Here’s an example that formats a number as a percentage:

double successRate = 0.85;
String message = `Success rate: ${successRate : %.2f%%}`;
System.out.println(message); // Output: Success rate: 85.00%

In this example, we use the : % format specifier to format the successRate variable as a percentage with two decimal places.

Escaping Expressions in String Templates

If you need to include a literal ${ or } character in a string template, you can escape it by doubling the braces:

String message = `This is a literal brace: ${{} and a literal dollar sign: $$`;
System.out.println(message); // Output: This is a literal brace: ${ and a literal dollar sign: $

Conclusion

String templates provide a powerful and concise way for Java developers to build and format complex strings. By embedding expressions directly within string literals, you can write cleaner, more maintainable code that is easier to read and understand. As you explore this new feature, you’ll find that string templates can help you streamline your string manipulation tasks and enhance the overall quality of your Java applications.

--

--

George Adams

Java Champion, Senior Engineer @Microsoft. Chairman @Adoptium. Views are my own.