π Introduction
In Java, we often build strings using +
, String.format()
, or StringBuilder
. While these work, they can be hard to read, prone to errors, and not very developer-friendly.
To fix this, Java 21 introduces a preview feature called String Templates β a new, modern way to format strings safely and clearly.
Itβs cleaner. Itβs smarter. Itβs safer.
In this blog, weβll break it down in simple terms for beginners.

π What Are String Templates?
String Templates are a new syntax for creating strings using placeholders. These placeholders are replaced with values at runtime β like modern templating in JavaScript or Python.
Java 21 introduces two important parts:
- Template expressions
- Processors
π§΅ Why Do We Need String Templates?
Letβs compare:
π οΈ Traditional Way (Verbose & Hard to Maintain)
1 2 3 4 5 6 7 |
String name = "Ketan"; int age = 30; String output = "Name: " + name + ", Age: " + age; |
π New Way Using String Templates
1 2 3 4 5 6 7 |
String name = "Ketan"; int age = 30; String output = STR."Name: \{name}, Age: \{age}"; |
Just like that β it looks clean and readable and maintainable!
π‘ How String Templates Work
Java uses template processors to interpret and replace placeholders (\{}
).
In Java 21, the built-in processor is:
1 2 3 4 5 |
STR."..." // uses StringTemplate.STR |
π§ͺ Example: Using String Templates
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public class Main { public static void main(String[] args) { String user = "Alice"; int score = 95; String message = STR."Hello \{user}, your score is \{score}."; System.out.println(message); } } |
β
Output: Hello Alice, your score is 95.
π Safety Benefits
String templates are:
- β Type-safe (catch errors at compile-time)
- β Null-safe
- β Cleaner and more readable
- β Secure (reduce injection risks in SQL, HTML)
π¦ Advanced: Custom Processors
Besides STR
, Java may allow custom processors for things like:
- HTML templates
- SQL queries
- Logging formats
But for now, just focus on STR.
for simple string output.
βοΈ Things to Remember
Feature | Supported? |
---|---|
Variables inside {} | β Yes |
Multi-line strings | β
Yes (with """ ) |
Template injection | β Prevented |
Requires Java 21 | β Yes (preview) |
π Template with Text Blocks
You can even use it with multiline strings:
1 2 3 4 5 6 7 8 9 10 |
String user = "Ketan"; String msg = STR.""" Dear \{user}, Welcome to Java 21! """; |
π§ͺ Enable in Java 21
Since this is a preview feature, you need to enable it while compiling:
1 2 3 |
javac --enable-preview --release 21 Main.java java --enable-preview Main |
π§Ύ Summary
- Java 21 introduces String Templates as a cleaner way to format strings.
- Use
STR."..."
with embedded placeholders like\{variable}
. - Itβs readable, safe, and beginner-friendly.
- Works with simple values and even multiline text blocks.
π¬ Final Thoughts
String Templates are a big step forward for modern Java.
If you’re learning Java 21, this is a great feature to adopt early. It makes your code cleaner, safer, and more expressive.
Reference : String Templates in Java 21