String manipulation is one of the most basic, yet most frequent tasks we write as developers. Whether you are building an API response, constructing a dynamic SQL query, or assembling an email template in a CRM service, how you manage strings impacts both code readability and runtime performance. Recently, the Java ecosystem went through a small rollercoaster regarding string features, causing some confusion. Let's clarify the state of modern string manipulation.
The Retraction of String Templates in JDK 23
In JDK 21, Java developers were thrilled to see String Templates (JEP 430) introduced as a preview feature. It allowed inline variable interpolation using syntax like:
String name = "Harshit";
String message = STR."Hello \{name}!";
However, in JDK 23, String Templates were officially retracted. The JDK team decided not to proceed with the current syntax. Why?
Feedback from the community and library authors highlighted several issues. The prefix syntax (like STR.) was felt to be somewhat clunky. More importantly, there were concerns about decoupling the string interpolation syntax from its underlying processor, which could lead to security risks like SQL injection if developers mistakenly used raw string interpolation instead of safety-ensuring template processors. The Java language architects chose to take a step back and redesign the feature to ensure it is robust, secure, and fits naturally into Java’s type system in a future release.
So, how should you format and manipulate strings in modern Java right now? Let's review the best practices.
Concatenation (+) vs. StringBuilder
For simple concatenations, the modern Java compiler is incredibly smart. If you write:
String full = "Welcome " + firstName + " " + lastName;
The compiler automatically optimizes this under the hood. In Java 9 and later, it uses invokedynamic (specifically calling StringConcatFactory) to allocate the memory once and concatenate the strings efficiently. You do not need to manually write StringBuilder for simple single-line concatenations.
However, the compiler cannot optimize concatenations that happen across loops. For example:
// BAD PRACTICE
String result = "";
for (String tag : tags) {
result += tag + ","; // Creates a new StringBuilder and String on each iteration!
}
In this scenario, a new StringBuilder is created on every loop iteration, which puts heavy pressure on the Garbage Collector. For loops, always use a StringBuilder:
// GOOD PRACTICE
StringBuilder sb = new StringBuilder();
for (String tag : tags) {
sb.append(tag).append(",");
}
String result = sb.toString();
Modern String Formatting: .formatted()
Using String.format("Hello %s", name) has been available since Java 5, but it is verbose. In Java 15, a cleaner instance method called .formatted(...) was introduced. It acts as a direct helper on any String object.
Here is how it simplifies your code:
String info = "User %s (ID: %d) logged in from IP %s".formatted(username, userId, ipAddress);
This is highly readable, compact, and is the recommended way to format variables inside string templates today.
Utilizing Java 15 Text Blocks
When writing multi-line strings—like JSON payloads for REST API tests, HTML emails, or SQL queries—developers used to escape every double quote (\") and add manual newlines (\n).
Introduced in Java 15, Text Blocks solve this completely. Text blocks use triple quotes (""") to wrap multi-line text without escaping.
Here is a clean example of writing dynamic JSON with Text Blocks and the .formatted() method:
String json = """
{
"employee": {
"name": "%s",
"role": "Senior Developer",
"location": "Bengaluru"
}
}
""".formatted(employeeName);
This keeps your code incredibly clean and maintainable.
Interview Strategy for Indian Software Engineers
During coding tests or technical reviews at companies like Capgemini, Infosys, or startups in Bengaluru, writing clean and optimized code is key. If you are asked to format strings, use Text Blocks and the .formatted() method to show your familiarity with modern Java. If you are asked about the performance of string concatenation in a loop, explain why StringBuilder is necessary to avoid GC churn, but clarify that single-line + concatenations are optimized by StringConcatFactory in modern Java. Knowing these nuances shows you have a deep, practical understanding of Java's runtime performance.