Quick revision
The short answer
String templates, written like STR."Hello \{name}", were a preview feature in JDK 21 (JEP 430) and again in JDK 22 (JEP 459), and they were then withdrawn: JDK 23 and every later release, including Java 25, do not contain them at all, so code that used them no longer compiles. In Java 25 we build strings with concatenation, String.format or formatted(), StringBuilder, or MessageFormat for localized text, and for SQL, JSON and HTML we use parameterized APIs and real serializers instead of building strings.
What to remember
In this answer, we saw that string templates were previewed in JDK 21 and JDK 22 through JEPs 430 and 459 and then withdrawn, so JDK 23 and later, including Java 25, do not have them and code that used them no longer compiles. Preview features need --enable-preview, are tied to one release and may change or vanish, which is why they stay out of production. Instead we use concatenation, formatted() or String.format, StringBuilder and MessageFormat, and for SQL, JSON and HTML we use bind parameters, serializers and escaping template engines.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
String templates, written like STR."Hello \{name}", were a preview feature in JDK 21 (JEP 430) and again in JDK 22 (JEP 459), and they were then withdrawn: JDK 23 and every later release, including Java 25, do not contain them at all, so code that used them no longer compiles. In Java 25 we build strings with concatenation, String.format or formatted(), StringBuilder, or MessageFormat for localized text, and for SQL, JSON and HTML we use parameterized APIs and real serializers instead of building strings.
Why this question is asked
Many developers tried string templates when Java 21 came out, and some blog posts and tutorials still show them as if they were part of the language. Interviewers ask about them to check whether we follow how Java actually evolves and whether we understand what preview features are. It is also a practical question: a team that used the preview in Java 21 now has code that cannot be built on any newer JDK. In this answer, we will recap what string templates were, see what happened to them in JDK 23, understand why preview code stops compiling, and then look at what to use instead, including for the injection-prone cases that templates were partly meant to solve.
1What String Templates Were
A string template is a string with embedded expressions. The proposal in JEP 430 used a backslash and braces for each expression and required a template processor in front of the string. The processor decided what to do with the literal parts and the values. STR simply joined them into a String, FMT also understood format specifiers, and RAW returned the unprocessed template so that a library could, for example, turn it into a safe SQL statement.
The processor idea was the interesting part. Because the processor saw the values separately from the text, a JSON or SQL processor could escape or bind each value correctly, which plain interpolation in other languages cannot do. JEP 459 in JDK 22 was a second preview with only small changes, collecting more feedback on that design.
2What Happened in JDK 23 and Later
During the JDK 23 cycle, the OpenJDK team decided that the design was not right, in particular the processor model and its syntax, and withdrew the feature instead of making it final. JDK 23 shipped without string templates, not even as a preview, and neither JDK 24 nor JDK 25 brought them back. The team said the design would be reworked, and as of Java 25 there is no new string template JEP in any release, so treat any future version as something to check when it appears rather than something to plan around.
| JDK | Status | What our code sees |
|---|---|---|
| 21 (LTS) | First preview, JEP 430 | Works with --enable-preview |
| 22 | Second preview, JEP 459 | Works with --enable-preview |
| 23 | Withdrawn | Compile error, even with --enable-preview |
| 24 | Not present | Compile error |
| 25 (LTS) | Not present | Compile error |
3Why Code That Used the Preview Stops Compiling
A preview feature is complete and tested, but not yet permanent. To use one, we must pass --enable-preview to javac together with the exact release, and again to java when running. The compiler marks the resulting class files as depending on preview features of that one release, and a JVM of any other release refuses to load them. So a class compiled with previews on JDK 21 does not run on JDK 22, even if the feature still existed there.
Notice that this is the deal preview features offer: we get early access in return for accepting that the feature can change or disappear and that our code is tied to one release. That is why preview features belong in experiments, not in production code or shared libraries. With string templates, the risk actually happened.
4What to Use Instead in Java 25
For ordinary messages, the tools that existed before templates are still the right ones. Simple concatenation with + is clear and fast; since JDK 9 the compiler turns it into an invokedynamic call to StringConcatFactory, which builds the string efficiently. For formatting numbers and padding, String.format() and its instance form formatted() (Java 15) are compact. Text blocks combine well with formatted() for multi-line output.
MessageFormat is the right choice for text that is translated, because the placeholders are numbered and translators can move them to fit the word order of their language. When we build a string in a loop, StringBuilder avoids creating a new string on every pass. Notice also the Locale.ROOT in the format call: format uses the default locale for numbers, which can change the decimal separator on some servers.
5SQL, JSON and HTML: Do Not Build Strings at All
Part of the motivation for template processors was safety: a processor could have bound SQL values properly. Without templates, the safe answer is the one we already had. For SQL we use bind parameters, so the value is never mixed into the SQL text. For JSON we serialize objects with Jackson, which escapes quotes and backslashes. For HTML we use a template engine such as Thymeleaf that escapes output by default.
So the replacement for templates is not one new API. It is the right tool for each target: plain formatting for human text, and a parameterized or serializing API for any text that another interpreter will read.
6Migrating Code That Used the Preview
If a codebase used templates on JDK 21, the migration is mechanical. We search for STR., FMT. and the \{ escape, replace each template with concatenation or formatted(), and remove --enable-preview from the compiler and runtime configuration so that no other preview use slips back in. Tests that compare the produced strings make the change safe, and it is a good moment to check whether any of those strings were SQL or JSON that should never have been built by hand.
7Follow-up questions
Will string templates come back in a later Java release?
Possibly, but in a different form. The OpenJDK team said the feature would be redesigned rather than abandoned for good, and there has been discussion of new approaches on the Amber project mailing lists. As of Java 25 no new version has been targeted to a release, so any code we write today should not depend on it. When a new JEP appears, it will start again as a preview, with the same rules.
Is string concatenation with + slow in modern Java?
Not for normal use. Since JDK 9, javac compiles a concatenation expression into an invokedynamic call that StringConcatFactory links to an efficient strategy at run time, which sizes the result once and copies the parts. The case to avoid is concatenating inside a loop, where each pass creates a new String; there we use StringBuilder or String.join, or Collectors.joining for streams.
What is the difference between String.format and formatted?
They do the same thing. formatted(args) is an instance method added in Java 15, so "%d items".formatted(3) is the same as String.format("%d items", 3) and reads more naturally after a text block. Both use the default locale unless we pass one to String.format, so for machine-readable output we use String.format(Locale.ROOT, ...) to avoid locale-specific digits and separators.
8Conclusion
In this answer, we saw that string templates were previewed in JDK 21 and JDK 22 through JEPs 430 and 459 and then withdrawn, so JDK 23 and later, including Java 25, do not have them and code that used them no longer compiles. Preview features need --enable-preview, are tied to one release and may change or vanish, which is why they stay out of production. Instead we use concatenation, formatted() or String.format, StringBuilder and MessageFormat, and for SQL, JSON and HTML we use bind parameters, serializers and escaping template engines.
Open with
String templates were a preview feature, first in JDK 21 with JEP 430 and again in JDK 22 with JEP 459. You wrote something like STR dot a string with backslash-brace expressions, and a template processor such as STR, FMT or RAW decided how to combine the text and the values.
Then, if they want more
During JDK 23 the OpenJDK team decided the design, especially the processor model, was not right, and withdrew it. So JDK 23, 24 and 25 do not contain string templates at all, not even as a preview, and the design is being reworked. Any code that used them no longer compiles on a newer JDK.
That is the deal with preview features: you need --enable-preview at compile and run time, the class files only run on that exact release, and the feature may change or disappear, which is why I keep previews out of production code and shared libraries.
In Java 25 I use plain concatenation, which is efficient since the compiler uses invokedynamic, String.format or the formatted method for numbers and padding, StringBuilder in loops, and MessageFormat for translated text where word order changes.
For anything another interpreter reads, I do not build strings: SQL uses bind parameters through PreparedStatement or JdbcClient, JSON goes through Jackson, and HTML through a template engine that escapes output.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 21, Java 25, Java 23, Java 22
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What happened to string templates (STR."...") in Java, and what should teams use instead...": https://scrivio-interview-library.netlify.app/java/strings/c920d20f9a/