String Templates in Java 21 (Preview, Later Withdrawn)
String templates were a preview feature in Java 21 (JEP 430) that let you embed expressions in strings, as in STR."Hello \{name}". After a second preview in Java 22 (JEP 459), the feature was withdrawn: it was not included in Java 23 and does not exist in later releases, including Java 25.
This tutorial explains how the Java 21 preview worked, why it was removed, and what to use in current code. Do not use string templates in new projects.
1. What Problem Did String Templates Address?
Building strings from values with + concatenation or String.format is either noisy or separates the text from the values. Many languages offer string interpolation. JEP 430 aimed to add it in a safe way: a template processor such as STR or FMT decided how the text and values were combined, which could also prevent injection attacks.
2. Syntax in the Java 21 Preview
String msg = STR."Order \{id}: \{qty} items"; // STR processor
String row = FMT."%-10s\{name} %8.2f\{price}"; // FMT with format specifiers
javac --release 21 --enable-preview TemplateBasics.java && java --enable-preview TemplateBasics
3. Practical Examples
Withdrawn feature: these examples compile only on JDK 21 or 22 with --enable-preview. They were compiled and run on JDK 21 for this tutorial. On JDK 23 and later they fail to compile.
Example 1: STR compared with formatted()
// Preview in Java 21 (JEP 430), second preview in Java 22, then withdrawn.
// Compiles only on JDK 21 or 22 with --enable-preview.
public class TemplateBasics {
record Order(String id, int qty, double price) { }
public static void main(String[] args) {
Order order = new Order("ORD-9", 3, 249.5);
// STR is imported automatically in Java 21 preview mode
String line = STR."Order \{order.id()}: \{order.qty()} x \{order.price()} = \{order.qty() * order.price()}";
System.out.println(line);
// Equivalent code without templates (works on every Java version)
String same = "Order %s: %d x %s = %s".formatted(order.id(), order.qty(), order.price(), order.qty() * order.price());
System.out.println(same);
System.out.println("Same text: " + line.equals(same));
}
}
Order ORD-9: 3 x 249.5 = 748.5 Order ORD-9: 3 x 249.5 = 748.5 Same text: true
Compile and run: javac --release 21 --enable-preview TemplateBasics.java && java --enable-preview TemplateBasics
Example 2: The FMT processor
import static java.util.FormatProcessor.FMT;
// Preview in Java 21 (JEP 430); withdrawn in Java 23
public class FmtProcessor {
public static void main(String[] args) {
String[] items = { "pen", "notebook", "ink" };
double[] prices = { 20, 60.5, 145.25 };
// FMT applies format specifiers placed before each embedded expression
for (int i = 0; i < items.length; i++) {
System.out.println(FMT."%-10s\{items[i]} %8.2f\{prices[i]}");
}
}
}
pen 20.00 notebook 60.50 ink 145.25
Compile and run: javac --release 21 --enable-preview FmtProcessor.java && java --enable-preview FmtProcessor
4. Timeline of the Feature
| Version | JEP | Status |
|---|---|---|
| Java 21 | JEP 430 | first preview |
| Java 22 | JEP 459 | second preview |
| Java 23 | JEP 465 | third preview proposed, then withdrawn; feature removed |
| Java 24 and 25 | - | not available |
5. What to Use Instead
"Order %s: %d".formatted(id, qty)(Java 15+) orString.format.StringBuilderfor building text in loops.MessageFormatfor localised messages.- Prepared statements (not string building) for SQL with user input.
6. Summary
- String templates were a preview in Java 21 (JEP 430) and Java 22 (JEP 459).
- They used processors such as
STRandFMTwith\{expression}placeholders. - The feature was withdrawn in Java 23 and is not part of Java 25.
- Use
formatted(),String.formatorStringBuilderinstead.
Related Tutorials
7. References
Frequently Asked Questions (FAQ)
Q1. Are string templates available in Java 25?
No. String templates were a preview in Java 21 and 22 and were withdrawn before Java 23, so they are not available in Java 23, 24 or 25.
Q2. What was the STR template processor?
A built-in processor in the Java 21 preview that replaced each \{expression} in a template with the value of the expression.
Q3. Why were string templates withdrawn?
The design, especially the template processor syntax, was reconsidered after feedback, so the preview was removed to allow a redesign rather than finalised.
Q4. What should I use instead of string templates?
Use String.formatted(), String.format(), StringBuilder or MessageFormat.