String templates were removed

Java 23 Course · lesson 2 of 15 · 4 min read

The most instructive thing in Java 23 is a feature that disappeared.

Open this lesson in the learning hub

Key points

  • String templates previewed in 21 and 22, then did not reappear in 23 - the JEP was withdrawn.
  • The design goal was sound: safe interpolation with processors that escape SQL or HTML properly.
  • The team concluded the syntax and the processor model needed rethinking rather than another preview.
  • Anyone who shipped STR."..." in a 22 build had to rewrite it to compile on 23.
  • This is the clearest possible argument for keeping preview features out of production code.

Example

// Java 21/22 preview syntax - does NOT compile on Java 23.
//   String greeting = STR."Hello \{name}, you have \{count} messages";
//
// The portable version, which has always worked and still does:
public class Main {
    public static void main(String[] args) {
        String name = "Ada";
        int count = 3;

        System.out.println("concat    : Hello " + name + ", you have " + count + " messages");
        System.out.println("formatted : " + "Hello %s, you have %d messages".formatted(name, count));
        System.out.println("MessageFormat and String.format also unaffected by the withdrawal");
    }
}

Preview features carry no compatibility promise - string templates proved it by vanishing.

This is a reading copy. The full lesson — with the visual explainer, the interactive lab and a Run button for the code — lives in the Java 23 Course course, and every lesson in it is listed on the Java 23 Course contents page.