top of page

Java String Templates

  • Mandip Dhakal
  • Sep 19, 2024
  • 3 min read

Updated: Mar 7

String Templates was introduced as part of Java 21 with JEP 430. In this blog, we will look at different approaches to string manipulation and how String Templates solve some of the common existing problems with the current implementation.


Scrabble tiles on a white background spell "CHOOSE YOUR WORDS" in a crossword style, emphasizing thoughtful communication.

Before we jump to the String Templates, let’s quickly go through the existing techniques to handle string composition operations.


  1. The “+” Operator: Java allows us to concatenate strings in different ways. The most common way is to use the ‘+’ operator. Each concatenation creates a new string which may lead to unnecessary memory allocations.


  2. Using String Builder: Another way is to use the StringBuilder class and use append() method to concatenate strings. Since StringBuilder is not thread-safe, it may lead to unexpected behavior in a multi-threaded environment due to synchronization problems.


  3. Using String Buffer: StringBuffer solves the problem of thread safety in a multi-threaded environment. However, due to synchronization, the performance is quite costly.


  4. Using String.format() method: String.format() method involves parsing and formatting at runtime, which may lead to a slight performance overhead.


  5. Using MessageFormat: It is often used to support localization. Since it requires pattern and format, it becomes less concise and may require additional code to set up the pattern.


  6. Using printf() method: System.out.printf() is suitable for printing formatted string values to the console but it doesn’t provide support to store the formatted string and use it later.


Java code snippet demonstrating six string concatenation methods: `+` operator, `StringBuilder`, `StringBuffer`, `String.format`, `MessageFormat`, and `printf`.
String templates complement Java's existing string literals and text blocks by coupling literal text with embedded expressions and template processors to produce specialized results.

Goal of String Templates:

  1. To simplify and make it easy to express strings that require computation at run time.

  2. To improve security by supporting validation and transformation on the template and the value of its embedded expressions.

  3. To enhance the readability of expression that has a mixture of text and expression whether it's a single or multi-line text.


Template Expressions: Template expressions are a new kind of expression in Java that can perform string composition and ensure safety. These expressions are made up of three main components: Template processors, A dot character, and a template. For example:

Code snippet on a white background showing "STR:" and "My name is \{name\}" in red text, indicating a string format.

where STR is a template processor, '.' is basically a dot character, and "My name is \{name}" is a template that contains an actual embedded expression which is \{name}.


Let's look at some use cases of these template processors with examples.


  1. RAW: Standard template processor that produces an unprocessed StringTemplate object.

Code snippet showing string template in Java with syntax highlighting. It processes and prints the text "My name is Joan" in green.

2. STR: It provides a shortcut way to process RAW template processor. With STR, we don't need to call the process method.

Code snippet showing string interpolation in Java. It assigns "John" to a name variable, creates a template, and prints "Hello John".

3. Multiple Lines and JSON support: The string template also provides support for multiple lines and creating a JSON string.

Code snippet showing JSON object creation with variables for name, phone, and address in Java. Includes JSON structure and output comment.

4. FMT: It is similar to STR processor, but it also interprets format specifiers that appear to the left of embedded expressions.

Code snippet formatting a table in Go. Variables: name, phone, address. Outputs formatted table with headings Name, Phone, Address, showing example data.

5. User-Defined Template Processor: We can also define our own template processor and use it according to our needs as shown below. In the below example, INTER is a custom template processor that interpolates x and y values.

Code snippet with Java code for string processing and calculation. Includes colorful syntax highlighting and output: "20 minus 5 equals 15".

6. Simplifying localization: String templates can also be used to simplify the localization process as shown below.

Code snippet showing a loop formatting numbers 1-5 using Thai digits. Includes commented output with Thai numerals.

Conclusion: Java String Templates

String templates are designed to handle string composition while ensuring safety and flexibility. It provides benefits while composing complex database queries, performing localization, and creating custom template processors. To use String Template, we need to enable preview mode.

Ready To Get Started?

Reach out to our professional support staff to help you build a brighter digital future.

bottom of page