00:00

String Templates in Java 21 (Preview, Later Withdrawn)

Last updated: · Preview in Java 21, 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

Syntax.java
String msg = STR."Order \{id}: \{qty} items";        // STR processor
String row = FMT."%-10s\{name} %8.2f\{price}";       // FMT with format specifiers
Commands (JDK 21 or 22 only)
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()

TemplateBasics.java
// 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));
    }
}
Output
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

FmtProcessor.java
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]}");
        }
    }
}
Output
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

VersionJEPStatus
Java 21JEP 430first preview
Java 22JEP 459second preview
Java 23JEP 465third preview proposed, then withdrawn; feature removed
Java 24 and 25-not available
How a template expression was evaluated in the preview A template expression was split into fragments and values; the template processor such as STR combined them into a String. STR."...\{x}..." template expression fragments + values StringTemplate object processor STR, FMT or custom result usually a String
Figure 1: The processor design was the novel part, and also the part that was later reconsidered.

5. What to Use Instead

  • "Order %s: %d".formatted(id, qty) (Java 15+) or String.format.
  • StringBuilder for building text in loops.
  • MessageFormat for 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 STR and FMT with \{expression} placeholders.
  • The feature was withdrawn in Java 23 and is not part of Java 25.
  • Use formatted(), String.format or StringBuilder instead.

Related Tutorials

7. References