Skip to main content

Command Palette

Search for a command to run...

Modern Java Feature: String Templates (Preview)

Updated
3 min readView as Markdown
Modern Java Feature: String Templates (Preview)
M

I’m a backend software engineer with over a decade of experience primarily in Java. I started this blog to share what I’ve learned in a simplified, approachable way — and to add value for fellow developers. Though I’m an introvert, I’ve chosen to put myself out there to encourage more women to explore and thrive in tech. I believe that by sharing what we know, we learn twice as much — that’s precisely why I’m here.

Update – December 2024:
String Templates were available as a preview feature in Java 21 and Java 22.
However, the feature was withdrawn from Java 23 because the JDK team decided it required more design work before moving forward.
The concept is not canceled — it is expected to return in a future release with an improved design — but it is not available in Java 23.

Everything described in this article refers to the earlier preview versions (Java 21–22). I’m keeping the article online for educational purposes and will update it again when the new design becomes available.

String Templates are one of the most anticipated additions to modern Java. They were first introduced as a preview feature in Java 21, refined again in Java 22, and kept in preview in Java 23. They are expected to remain in preview until at least Java 24 or 25, when the design stabilizes enough to be finalized.

Even as a preview feature, String Templates are worth trying out. They significantly improve how developers construct strings in Java, and as a result, you will be better prepared when they are finalized.

What is this feature about?

String Templates allow embedding Java expressions directly inside string literals using the STR processor. This provides cleaner syntax for building structured strings and complements Text Blocks, making Java feel more expressive and modern.

Quick example:

String name = "Mirna";
String message = STR."Hello, \{name}! Today is \{LocalDate.now()}";
System.out.println(message);

Sample output:

Hello, Mirna! Today is 2025-01-01

How to run preview features:
If you are trying this with Java 21 or newer, you must enable preview mode, for a class for code that uses this feature like StringTemplatesDemo, we will need:

javac --enable-preview --release 21 StringTemplatesDemo.java
java  --enable-preview StringTemplatesDemo

Adjust the version number if you use a newer JDK (22, 23, etc.).


Common Use Cases

1. Build JSON cleanly

String user = "Mirna";
int score = 98;

String json = STR."""
{
  "user": "\{user}",
  "score": \{score},
  "active": true
}
""";

System.out.println(json);

2. Construct SQL dynamically

String table = "users";
String email = "mirna@example.com";

String query = STR."""
SELECT *
FROM \{table}
WHERE email = '\{email}'
""";

System.out.println(query);

3. Build HTML fragments

String name = "Mirna";

String html = STR."""
<p>Hello, <b>\{name}</b>!</p>
""";

System.out.println(html);

4. Use expressions directly

String math = STR."2 + 2 = \{2 + 2}";
String random = STR."Random value: \{Math.round(Math.random() * 10)}";

System.out.println(math);
System.out.println(random);

5. Use methods inside templates

String greet(String name) {
    return STR."Hello, \{name.toUpperCase()}!";
}

System.out.println(greet("Mirna"));

6. Combine Text Blocks + Templates

String language = "Java";
String feature = "String Templates";

String article = STR."""
## Feature Highlight

We're exploring \{feature} in \{language}.

Advantages:
- Readable
- Multiline
- Interpolated directly in code
""";

System.out.println(article);

Final Thoughts

String Templates make Java more modern and readable, especially when combined with Text Blocks. The feature is a meaningful improvement over string concatenation and String.format.

There are still two things that feel a bit unnatural to me.
First, the backslash before the curly braces (\{name}). Second, having to prefix a Text Block with STR.""" just to enable interpolation. Most modern languages do not require this extra step. I can live with both, though, since the improvement in readability is real.

I’m excited to see how this feature evolves as it moves toward finalization.

More from this blog

Code Like a Woman

40 posts

Code, learnings, and experiences from a Latina in tech.