# Modern Java Feature: String Templates (Preview)

> **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:**

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

**Sample output:**

```java
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:

```java
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

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

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

System.out.println(json);
```

## 2\. Construct SQL dynamically

```java
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

```java
String name = "Mirna";

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

System.out.println(html);
```

## 4\. Use expressions directly

```java
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

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

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

## 6\. Combine Text Blocks + Templates

```java
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.
