r/java • • Jul 17 '26

Discussion: What is the future of String Interpolation in Java?

Java has always been conservative when it comes to language changes, and I think string interpolation is an interesting example.

Many modern languages provide native interpolation syntax:

JavaScript:

\${name}``

Python:

f"{name}"

Kotlin:

"$name"

C#:

$"{name}"

These features are not only about reducing characters. They improve readability when building dynamic text.

Java explored this direction with String Templates (JEP 430), but I haven't seen much discussion recently about where this feature is heading.

For developers working with large Java applications, string construction appears everywhere:

- logging messages

- SQL/debug output

- API descriptions

- test cases

- generated configuration

- user-facing messages

I am curious about the community's opinion:

  1. Do you think Java still needs native string interpolation?
  2. Should String Templates continue evolving?
  3. What syntax direction would feel most "Java-like"?

For example:

"Hello, \{name}"

or:

"Hello, ${name}"

Personally, I think Java does not need to copy other languages, but a better way to express dynamic strings would improve developer experience.

What do you think?

56 Upvotes

119 comments sorted by

View all comments

Show parent comment

1

u/vytah Jul 24 '26

I tested a bit more how the overload resolution works and it's a bit more fucked that either you or I thought.

Here's an example:

using System;
using System.Runtime.CompilerServices;
[InterpolatedStringHandler]
public struct Foo
{
    public Foo(int literalLength, int formattedCount) =>
        Console.WriteLine("Creating Foo");
    public void AppendLiteral(string s) {}
    public void AppendFormatted<T>(T t) {}
    public override string ToString() => "";
}
class Program
{
    const string B = "b";
    static void Bar(string x) => Console.WriteLine("string " + x);
    static void Bar(Foo x) => Console.WriteLine("Foo");
    static void Main() {
        string b = "b";
        Bar($"abc");
        Bar($"a{"b"}c");
        Bar($"a{b}c");
        Bar($"a{B}c");
    }
}

Guess what it prints.

string abc
string abc
Creating Foo
Foo
string abc

So the string overload is preferred when there are no parameters (a bit confusing, but I guess fine?) or when the parameters are constants (definitely not fine). So,

In your example, the MagicalSafeHtml overload would still be used for interpolated strings.

it actually depends whether text is a constant string or not.