javastringstring-interpolationjava-15

String interpolation in Java 14 or 15


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)

Since the answer which I got from lots fo references are old answers asked 4,5 years ago. Is there any update on the string interpolation in java 11,12,13,14,15 equivalent to C#

string name = "Horace";
int age = 34;
Console.WriteLine($"Your name is {name} and your age {age}");

EDIT: JEP 430, which covers this and much more, has been proposed to target JDK 21.


Solution

  • There is something slightly closer; an instance version of String::format, called formatted:

    String message = "Hi, %s".formatted(name);
    

    It is similar to String::format, but is more friendly to use in chained expressions.