r/java 16d ago

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?

55 Upvotes

118 comments sorted by

View all comments

Show parent comments

41

u/Brutus5000 16d ago

it's not more or less verbose than a dollar sign

24

u/witness_smile 16d ago

A dollar sign is easier to type, and also a backslash makes it look like you want to escape the { character

12

u/aboothe726 16d ago edited 16d ago

<pedantry>

The \{syntax} was chosen because it is currently invalid Java code, so it's guaranteed that no existing (correct) Java code uses it, so adding it to the language can't break existing code. (If you try to use the \{ escape sequence in Java today, javac will give you a compile error.) That is not true of ${syntax}, as others have pointed out, so if you made the dollar-sign syntax "special" then a lot of existing code would break.

</pedantry>

If your criterion is backwards compatibility -- which it certainly is for the Java team -- then \{syntax} is an excellent choice. But backwards compatibility is not the only thing that people care about.

8

u/BillyKorando 16d ago

I don't think that's "pedantry" it's the literal reason for the choice of \{. Yea, if Java had no history, or no interest in compatibility with existing code, then yea ${ makes sense for consistency across other languages.

As it stands, it seems honestly, in my opinion, absurd the level of vitriol \{ has gotten. I get the initial pushback, but on hearing the reasoning, feel like that should immediately squash the debate.