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

🌐
Baeldung
baeldung.com › home › java › java string › string interpolation in java
String Interpolation in Java | Baeldung
July 23, 2025 - As we can see in the above code example, we may interpolate the strings with the necessary text by chaining the append function, which accepts the parameter as a variable (in this case, two Strings). Using the MessageFormat class is a lesser known method to obtain String interpolation in Java.
Discussions

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
Design: String Interpolation vs printf() with Format Strings (which is better/cleaner?)
I think interpolation is far better. It also saves you from the type safety issues that often plague printf implementations in various ways. Note that if you want to support high quality localisation libraries, you may still need to support printf-like functions, although they don't need to be quite as ergonomic, and can support printing only strings. More on reddit.com
🌐 r/ProgrammingLanguages
55
22
October 6, 2024
What Happened to Java's String Templates? Inside Java Newscast
Honestly, this whole thing with string templates in java feels like a paranoia. Security? Validation? The hell are they smokin there? Why are they trying to solve world hunger with it? Just give people the damn interpolation like all normal human beings have other languages that's all we want. More on reddit.com
🌐 r/java
122
66
May 16, 2024
Raw interpolated multiline string template, all in one
How often does one need to include a sequence of 3 double quotes in a long string quote inside a source file? I'm guessing 'Almost never, but it would be nice if one could'. How often is 'the spec says you can use any amount of quotes as opener/closer and IDEs need to keep that in mind' going to cause problems that 'its always 3 to open a long string and 3 to close one' wouldn't? I'm guessing 'Almost never, but it'd be nice to avoid the problem entirely'. So, dingdingding! It's round 1 of the big battle: Who wins? Is it the first 'Almost never' or the second 'Almost never'? And the winner is....: I'm not actually sure all that many people care one way or another, to be honest. Maybe I'm missing some significant use case. For what its worth, posting stuff like this to reddit accomplishes absolutely nothing. Posting a ton of text for almost no actual context (the only point you're really making is the any-character thing (item #4), as 1-3 are already addressed) to the relevant mailing list on openjdk.net probably also accomplishes nothing. Posting something short and sweet might, but note that all this stuff has been talked about at length, so if you just post this without reviewing all that, you'll be laughed out of the mailing list. It's not a very friendly place, so, you know. Come prepared, know your shit, and make damn sure you've read up on the extremely extensive conversations that preceded you - and bring more arguments than stuff that's already been mentioned before, or arguments that boil down to 'But I, the great sken130, decree that it is important. Why won't you take my word for it?'. For example, come up with a solid use case and go hunt github and the like for projects that really would benefit from your idea. Unfortunately, in my experience, if you jump through all those hoops, team OpenJDK still completely ignores all solid and well researched advice and just does what oracle wants. They talk the talk (they talk the talk a lot, sjeesh), but really don't walk the walk. In my experience anyway. My point is, it's reasonable for team OpenJDK to demand that [A] such conversations are done on openJDK mailing lists and not on reddit or any other platform, [B] that you don't just wade in there and demand somebody spend time rehashing for the 15th time the same arguments against over and over (i.e. that you read up and continue the conversation instead of starting anew), and [C] that you bring a little more to the table than your say-so. Which this reddit rant isn't. More on reddit.com
🌐 r/java
19
7
November 22, 2023
🌐
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.
🌐
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.

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
🌐
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)
To include the result of an expression in a string, prefix the expression with $(and follow it with ). For example, the following two strings are identical after interpolation:
🌐
Wikipedia
en.wikipedia.org › wiki › String_interpolation
String interpolation - Wikipedia
February 10, 2026 - Java had interpolated strings as a preview feature in Java 21 and Java 22. One could use the constant STR of java.lang.StringTemplate directly. package org.wikipedia.examples; enum Stage { TEST, QA, PRODUCTION } record Deploy(UUID image, Stage stage) {} public class Example { public static void main(String[] args) { Deploy deploy = new Deploy(UUID.randomUUID(), Stage.TEST) STR."Installing \{deploy.image()} on Stage \{deploy.stage()} ..." Deploy deploy = new Deploy(UUID.randomUUID(), Stage.PRODUCTION) STR."Installing \{deploy.image()} on Stage \{deploy.stage()} ..." } }
Find elsewhere
🌐
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)
To include the result of an expression in a string, prefix the expression with $(and follow it with ). For example, the following two strings are identical after interpolation:
🌐
Oracle
docs.oracle.com › cd › E19146-01 › 820-2203 › 6ndqqsc9k › index.html
String Interpolation (Sun Java System Web Server 7.0 Update 2 Administrator's Configuration File Reference)
To include the result of an expression in a string, prefix the expression with $(and follow it with ). For example, the following two strings are identical after interpolation:
🌐
Scaler
scaler.com › home › topics › java string interpolation
Java String Interpolation - Scaler Topics
December 20, 2022 - There are primarily five ways to implement Java String Interpolation, which includes using plus (+) operator, String.format() method, MessageFormat class, and StringBuilder class. Java string interpolation can be achieved by using + operators by concatenating strings with variables.
🌐
TutorialsPoint
tutorialspoint.com › java-program-to-illustrate-string-interpolation
Java Program to Illustrate String Interpolation
November 19, 2024 - 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.
🌐
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. Here’s an example of using FMT for creating a text report.
🌐
GoLinuxCloud
golinuxcloud.com › home › java › 5 methods to perform string interpolation in java
5 Methods to perform String Interpolation in Java | GoLinuxCloud
July 21, 2022 - When the variable name is placed outside the double quotes, it will be replaced by the value of that variable in the output string. Example : In this example, we are scanning a name from user and then print the customized welcome message.
🌐
Oracle
docs.oracle.com › cd › E19316-01 › 820-6599 › abvaw › index.html
String Interpolation (Sun Java System Web Server 7.0 Update 4 Administrator's Configuration File Reference)
August 14, 2023 - To include the result of an expression in a string, prefix the expression with $(and follow it with ). For example, the following two strings are identical after interpolation:
🌐
Oracle
docs.oracle.com › cd › E19146-01 › 821-0794 › abvaw › index.html
String Interpolation (Sun Java System Web Server 7.0 Update 7 Administrator's Configuration File Reference)
October 14, 2025 - To include the result of an expression in a string, prefix the expression with $(and follow it with ). For example, the following two strings are identical after interpolation:
🌐
Oracle
docs.oracle.com › cd › E19146-01 › 819-2630 › abvaw › index.html
String Interpolation (Sun Java System Web Server 7.0 Administrator's Configuration File Reference)
May 20, 2023 - To include the result of an expression in a string, prefix the expression with $(and follow it with ). For example, the following two strings are identical after interpolation:
🌐
Baeldung
baeldung.com › home › java › java string › string templates in java
String Templates in Java | Baeldung
July 30, 2024 - 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. We’ll apply the STR processor String template in our previous example ...
🌐
DEV Community
dev.to › nermin_karapandzic › string-interpolation-in-java-finally-74g
String Interpolation in Java, finally - DEV Community
December 3, 2023 - However, none of these methods truly paralleled the ease of real string interpolation, a feature commonplace in most other programming languages. C# $"{x} plus {y} equals {x + y}" Visual Basic $"{x} plus {y} equals {x + y}" Python f"{x} plus {y} equals {x + y}" Scala s"$x plus $y equals ${x + y}" Groovy "$x plus $y equals ${x + y}" Kotlin "$x plus $y equals ${x + y}" JavaScript `${x} plus ${y} equals ${x + y}` Ruby "#{x} plus #{y} equals #{x + y}" Swift "\(x) plus \(y) equals \(x + y)"
🌐
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 - We get entirely new syntax to indicate that interpolation is happening and we still have to throw around special characters to indicate that something is a string interpolation? Why? There's no backwards compatibility concerns! And we need to use a different template processor to get formatting options for floats etc? What are they smoking? ... Dafuq are you talking about? FMT is there for fine-grained control over printing. You can use floats with STR as is. ... I am talking about the exact thing you mentioned, mate. ... Really a shame this got pulled in at all. There will be no String Template in JDK 23.
🌐
GitHub
github.com › antkorwin › better-strings
GitHub - antkorwin/better-strings: Java String Interpolation Plugin
April 27, 2023 - The Java Plugin to use string interpolation for Java (like in Kotlin).
Starred by 99 users
Forked by 8 users
Languages   Java 100.0% | Java 100.0%