Master Java 21 Template Expressions for Clean String Interpolation
This article introduces Java 21's new Template Expressions, explains how STR and FMT processors enable safe, efficient string interpolation and formatting—including multi‑line strings, arithmetic, method calls, and object fields—while providing practical code examples and best‑practice guidance.
When writing Java code, string manipulation is common, and historically developers have used concatenation with +, StringBuffer / StringBuilder, String::format / String::formatted, or java.text.MessageFormat. Java 21 adds a new approach called Template Expressions (STR), which supports string interpolation and can also transform structured text into objects.
Template Expressions
Template Expressions allow developers to embed expressions inside a string using the syntax \{expression}. The STR processor evaluates these expressions and produces the final string.
String blog = "blog.didispace.com";
String str = STR."My blog is \{blog}";The example above contains three parts: the STR processor ( STR), the template with an embedded expression ( \{blog}), and the dot operator that combines them. At runtime the result is My blog is blog.didispace.com.
Multi‑line Template Expressions
Similar to Java text blocks, multi‑line templates let developers compose HTML, JSON, XML, etc., in a readable way.
var json = STR.""
{
"user": "\{name}",
"age": \{age}
}"";STR Processor Capabilities
Beyond simple interpolation, STR can perform arithmetic, call methods, and access object fields.
int x = 10, y = 20;
String s = STR."\{x} + \{y} = \{x + y}"; // results in "10 + 20 = 30" String s = STR."My blog is \{getMyBlog()}"; // assuming getMyBlog() returns "blog.didispace.com" User u = new User("didi", "blog.didispace.com");
String s = STR."\{u.name}的博客地址为:\{u.blog}"; // results in "didi的博客地址为:blog.didispace.com"FMT Processor
The FMT processor provides the same interpolation ability as STR but also supports left‑aligned formatting for each column, useful for generating aligned tables.
String table = FMT.""
Description Width Height Area
%-12s\{zone[0].name} %7.2f\{zone[0].width} %7.2f\{zone[0].height} %7.2f\{zone[0].area()}
%-12s\{zone[1].name} %7.2f\{zone[1].width} %7.2f\{zone[1].height} %7.2f\{zone[1].area()}
%-12s\{zone[2].name} %7.2f\{zone[2].width} %7.2f\{zone[2].height} %7.2f\{zone[2].area()}
%".repeat(28) Total %7.2f\{zone[0].area() + zone[1].area() + zone[2].area()}
"";The formatted output aligns each column neatly, producing a clean table of descriptions, widths, heights, and areas.
Java 21's String Templates are still in preview, and further capabilities such as custom processors are expected. Readers are encouraged to explore the official documentation for deeper insights.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
