If you're using Java 5 or higher, you can use String.format:

urlString += String.format("u1=%s;u2=%s;u3=%s;u4=%s;", u1, u2, u3, u4);

See Formatter for details.

Answer from Jon Skeet on Stack Overflow
🌐
Baeldung
baeldung.com › home › java › java string › string templates in java
String Templates in Java | Baeldung
July 7, 2025 - Java provides some out-of-the-box template processors. The STR Template Processor performs string interpolation by iteratively replacing each embedded expression of the provided template with the stringified value of that expression.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-to-illustrate-string-interpolation
String Interpolation in Java - GeeksforGeeks
July 23, 2025 - Below is the simplest way to do string interpolation using "+" operator in Java.
Discussions

Interpolating Strings Like a King in Java 21
Pretty sad that this got through. .. always imported as private static final field. Why. I thought we've already learned from magic fields such as serialUID that they're a bad idea. Now not only I will have to point out people are incorrectly concatenating strings, but also that they do not escape their special characters. Wonderful! More on reddit.com
🌐 r/java
19
41
April 27, 2023
String interpolation in Java 14 or 15 - Stack Overflow
Since I am using Java 14 and 15 preview features. Trying to find the string interpolation in java. The closest answer I found is String.format("u1=%s;u2=%s;u3=%s;u4=%s;", u1, u2, u3, u4) ... More on stackoverflow.com
🌐 stackoverflow.com
Java String Interpolation final in Java 23
The syntax is really ugly, but it's nice to finally get string interpolation in regular old Java. More on reddit.com
🌐 r/programming
7
13
January 13, 2024
Why does Java's string templating use clumsy STR prefix? - Stack Overflow
Java finally supports string interpolation! I'm very happy about it. However, the syntax seems to me a bit clumsy // string templating syntax from Java 23 String greeting = STR."Hello \{name}... More on stackoverflow.com
🌐 stackoverflow.com
Top answer
1 of 5
442

If you're using Java 5 or higher, you can use String.format:

urlString += String.format("u1=%s;u2=%s;u3=%s;u4=%s;", u1, u2, u3, u4);

See Formatter for details.

2 of 5
147

Edit March 2024

Many things have changes for the past ten years. Java currently has JEP 430: String Templates in the preview feature (Java 21 & Java 22). String templates are not equivalent to string interpolation, but they support it. String templates are much more powerful than traditional string interpolations in C#, Perl, or Python.

String templates have built-in string processors (STR and FMT) and also allow to create custom template processors. They are, according to the documentation, safer (or allow to create safer code more easily).

import static java.util.FormatProcessor.FMT;

void main() {

    int age = 34;
    String name = "William";

    String msg = STR."\{name} is \{age} years old";
    System.out.println(msg);

    String msg2 = FMT."%s\{name} is %d\{age} years old";
    System.out.println(msg2);
}

The example demonstrates the usage of STR and FMT processors. The latter must be explicitly imported.

From the JEP:

STR is a template processor defined in the Java Platform. It performs string interpolation by replacing each embedded expression in the template with the (stringified) value of that expression.

FMT is another template processor defined in the Java Platform. FMT is like STR in that it performs interpolation, but it also interprets format specifiers which appear to the left of embedded expressions.

In essence, Java now supports string interpolation in preview within a more powerful concept called string templates.

The following information is outdated now and reflects the situation before Java 21:

Note that there is no variable interpolation in Java. Variable interpolation is variable substitution with its value inside a string. An example in Ruby:

#!/usr/bin/ruby

age = 34
name = "William"

puts "#{name} is #{age} years old"

The Ruby interpreter automatically replaces variables with its values inside a string. The fact, that we are going to do interpolation is hinted by sigil characters. In Ruby, it is #{}. In Perl, it could be $, % or @. Java would only print such characters, it would not expand them.

Variable interpolation is not supported in Java. Instead of this, we have string formatting.

package com.zetcode;

public class StringFormatting 
{
    public static void main(String[] args) 
    {
        int age = 34;
        String name = "William";

        String output = String.format("%s is %d years old.", name, age);
    
        System.out.println(output);
    }
}

In Java, we build a new string using the String.format() method. The outcome is the same, but the methods are different.

See http://en.wikipedia.org/wiki/Variable_interpolation

Edit As of 2019, JEP 326 (Raw String Literals) was withdrawn and superseded by multiple JEPs eventually leading to JEP 378: Text Blocks delivered in Java 15.

A text block is a multi-line string literal that avoids the need for most escape sequences, automatically formats the string in a predictable way, and gives the developer control over the format when desired.

However, still no string interpolation:

Non-Goals: … Text blocks do not directly support string interpolation. Interpolation may be considered in a future JEP. In the meantime, the new instance method String::formatted aids in situations where interpolation might be desired.

🌐
Medium
medium.com › @viraj_63415 › java-21-string-templates-79fd908f30ff
Java String Templates — A Better Interpolation | by Viraj Shetty | Medium
June 19, 2024 - There are several inbuilt Template Processors available in Java. STR is one of the inbuilt Template Processors that can be used to evaluate the String Template. Apart from STR, there are two other Template Processors available — FMT and RAW. FMT performs String interpolation but it also interprets format specifiers which appear to the left of embedded expressions.
🌐
Oracle
docs.oracle.com › en › java › javase › 21 › docs › api › java.base › java › lang › StringTemplate.html
StringTemplate (Java SE 21 & JDK 21)
January 20, 2026 - The interpolate() method provides a direct way to perform string interpolation of a StringTemplatePREVIEW.
🌐
Reddit
reddit.com › r/java › interpolating strings like a king in java 21
r/java on Reddit: Interpolating Strings Like a King in Java 21
April 27, 2023 - I agree the $ would be nice, too. I think \ is a bit confusing because it means "escape" in Java, so I would expect it to disable string interpolation.
🌐
Baeldung
baeldung.com › home › java › java string › string interpolation in java
String Interpolation in Java | Baeldung
July 23, 2025 - First, we have the “+” operator. We can use the “+” operator to concatenate our variables and string values. The variable is replaced by its value, so we achieve interpolation or concatenation of strings:
Find elsewhere
🌐
Reddit
reddit.com › r/programming › java string interpolation final in java 23
r/programming on Reddit: Java String Interpolation final in Java 23
January 13, 2024 - The syntax is really ugly, but it's nice to finally get string interpolation in regular old Java.
🌐
Oracle
docs.oracle.com › en › java › javase › 21 › language › string-templates.html
Java Language Updates
January 16, 2025 - It automatically performs string interpolation by replacing each embedded expression in the template with its value, converted to a string. The JDK includes two other template processors: The FMT Template Processor: It's like the STR template processor except that it accepts format specifiers ...
🌐
OpenJDK
openjdk.org › jeps › 459
JEP 459: String Templates (Second Preview)
August 14, 2023 - Composing a query string with simple-minded interpolation is just as unsafe as composing it with traditional concatenation: String query = "SELECT * FROM Person p WHERE p.last_name = '" + name + "'"; For the Java Platform, we would like to have a string composition feature that achieves the clarity of interpolation but achieves a safer result out-of-the-box, perhaps trading off a small amount of convenience to gain a large amount of safety.
🌐
Belief Driven Design
belief-driven-design.com › looking-at-java-21-string-templates-c7cbc
Looking at Java 21: String Templates | belief driven design
June 20, 2023 - The result I want to show you here might be different from other languages’ simple String interpolation, but in return, it gives us a more flexible and versatile scaffold. The new way to work with Strings in Java is called template expression, a programmable way of safely interpolating ...
🌐
nipafx
nipafx.dev › inside-java-newscast-71
What Happened to Java's String Templates? Inside Java Newscast #71 // nipafx
PEP 501 starts by pointing out that code using it is "superficially elegant" but also "an opening for a form of code injection attack". It then proposes a new kind of expression and an InterpolationTemplate type that look very similar to the string template previews in JDK 21 and 22. So did Python copy Java here?
Published   June 20, 2024
🌐
TutorialsPoint
tutorialspoint.com › article › java-program-to-illustrate-string-interpolation
Java Program to Illustrate String Interpolation
The following program is written to demonstrate the usage of String Interpolation using the format() method. // Java Program to Illustrate the working of String Interpolation // by the means of the format () method public class StringInterpolation2 { public static void main(String[] args){ // String 1 String str1 = "Let us make the world"; // String 2 String str2 = "to live "; // display the interpolated string System.out.println(String.format("%s a better place %s in.", str1,str2)); } }
🌐
DEV Community
dev.to › sharique_siddiqui_8242dad › understanding-string-interpolation-in-java-34ch
Understanding String Interpolation in Java - DEV Community
July 30, 2025 - In the world of programming, string interpolation is a handy feature that allows developers to embed variables directly into strings. It makes code more readable, concise, and easier to maintain. While many languages like Python, JavaScript, and Kotlin have built-in support for string interpolation using symbols like $ or {}, Java doesn’t support native string interpolation — yet.
🌐
Delft Stack
delftstack.com › home › howto › java › java string interpolation
How to Do String Interpolation in Java | Delft Stack
February 2, 2024 - It is the simplest approach. We can use + to perform the string interpolation. Java uses the + operator to concatenate variables with the string. So we can use it for string interpolation as well.
🌐
Oracle
docs.oracle.com › cd › E19146-01 › 821-1497 › abvaw › index.html
String Interpolation (Sun Java System Web Server 7.0 Update 8 Administrator's Configuration File Reference)
Use interpolated strings in expressions, log formats, and obj.conf parameters. In expressions, only string literals bracketed by double quotes are interpolated.
🌐
DEV Community
dev.to › nermin_karapandzic › string-interpolation-in-java-finally-74g
String Interpolation in Java, finally - DEV Community
December 3, 2023 - In this brief discourse, I'd like to highlight one of my favorites – the introduction of a user-friendly string interpolation feature known as string templates. Up until now, the Java ecosystem had traversed various methods for string composition:
🌐
Oracle
docs.oracle.com › cd › E19146-01 › 820-1062 › abvaw › index.html
String Interpolation (Sun Java System Web Server 7.0 Update 1 Administrator's Configuration File Reference)
Use interpolated strings in expressions, log formats, and obj.conf parameters. In expressions, only string literals bracketed by double quotes are interpolated.