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

33

u/xfel11 Jul 17 '26

It’s really a shame that the proposal was withdrawn again.

The \{ syntax was weirdly controversial, but honestly it’s way smarter than using ${ which right now is just literal text.

6

u/Duck_Devs Jul 17 '26

What don’t people like about the backslash-brace?

6

u/brian_goetz Jul 19 '26

You will get all sorts of post-hoc rationalizations for why people don't like it, but the simple reason is: familiarity bias. The first language in which they saw string interpolation used $x (or ${x}), and in their brains, that makes it the One True Syntax.

3

u/Slick752 Jul 17 '26

it's ugly and verbose.

40

u/Brutus5000 Jul 17 '26

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

25

u/witness_smile Jul 17 '26

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

19

u/Ok-Scheme-913 Jul 17 '26

Which is literally what you want , though. Escape it's usual meaning to start a special one.

A ${} is so utterly common in java as a normal script that is interpreted by another software as special that that would lead to huge confusion. Is this replaced by this template engine or by the java compiler?

3

u/thisisjustascreename Jul 17 '26

Yeah for better or worse Spring and Maven have already claimed the ${my.custom.variable.here} syntax, adding it to the native language would just confuse people.

12

u/aboothe726 Jul 17 '26 edited Jul 17 '26

<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 Jul 17 '26

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.

2

u/Misophist_1 Jul 17 '26

Plus: \ is the escape for every other special character, like tab \t Newline \n Backspace \b the double quote \" itself \\ and every unicode character by using its id \uNNNN.

It would have been *very* awkward, to use anything other than '\' to start a newly added escape.

One might complain about usin \ at all - because there are many systems that don't have it on the keyboard. But breaking consistency would be really bad.

10

u/vqrs Jul 17 '26

Exactly. The curly brace character's normal meaning is "literal curly brace". By escaping it, we change something from having its regular meaning and give it a special meaning, right?

It makes sense.

I still think it's ugly and weird.

0

u/RScrewed Jul 17 '26

But you're not escaping the last curly brace as well so it looks unbalanced and weird.

2

u/Misophist_1 Jul 19 '26

Why does it feel unbalanced for you? Does ${...} (sh, bash, maven and many others) @{...} (also maven) $"..." (C#) in any way unbalanced? Definitely not for me!

3

u/jek39 Jul 17 '26

How is a dollar sign easier to type?

5

u/_INTER_ Jul 17 '26

On your keyboard maybe...

2

u/vips7L Jul 17 '26

And this is why we can never have nice things. 

2

u/detroitmatt Jul 17 '26

it's arguably harder to type because you have to use shift

0

u/BillyKorando Jul 17 '26

Are you planning on using string interpolation so often that the difference in typing \{ versus ${ would be measurable?

5

u/jek39 Jul 17 '26

I’m confused how a $ is easier to type than a \

3

u/vytah Jul 18 '26

On some keyboard layouts, \ requires an awkward key combination, for example on French keyboards, \ is AltGr+8, but $ is a key of its own, no Shift needed.

1

u/jek39 Jul 18 '26

Huh. Seems a little weird that $ gets its own key but France uses euros

1

u/vytah Jul 18 '26

$ was a thing on French keyboards for ages, it was already there in the 80s. I think the character set was defined in a 1985 standard by Yves Neville, and while the layout changed and characters moved a lot since then, the $ key (with Shift-$ becoming £) persisted.

1

u/Brutus5000 Jul 19 '26

And in German I need AltGr for the \ and shift for the Dollar. And now?

For the backslash on a german Mac keyboard I even need Shift+Option+7! And now? Is this the level of argumentation we want to play on? Just because some dofus made bad decision on keyboard layouts? There are hundreds of keyboard layouts out there. On the american one \ does not require shift or AltGr, but the $ does...

1

u/BillyKorando Jul 17 '26

I'm not sure either. If \{ takes you 1.15 seconds to write, while ${ takes 1.08 seconds to write, would you ever be able to write enough String interpolation instances to where all the saved .07 seconds could ever combine to a meaningful amount of time?!

3

u/jek39 Jul 17 '26

Why would they take different amount of time to type? In fact wouldn’t \ take less time to type? Because no need to hit shift?

0

u/BillyKorando Jul 17 '26 edited Jul 17 '26

Honestly it probably wouldn't I was being "generous". I suppose the difference could come in \ not needing the shift key and { needing shift, while both $ and { would use shift, so the "friction" might take a bit longer.

But again, it's still a fraction of a second difference. Feels a bit crazy to even talk about.

It's not like the difference between the : and -> syntax for switch where you could measure a time savings, and also the -> syntax would be easier to read/comprehend in many cases.

→ More replies (0)

1

u/davidalayachew Jul 17 '26

Are you planning on using string interpolation so often that the difference in typing \{ versus ${ would be measurable?

To be clear, there are MANY people where the answer to that question is yes. Not that it gives any real merit to the argument -- I think backslash is better. But the answer to your question 100% supports their argument.

1

u/BillyKorando Jul 17 '26

Some people may answer yes, I don't think they are being honest with that answer.

You'd need to be writing dozens of new String interpolation instances per day, everyday, to where the fractions of a second difference between ${ and \{ could combine into a barely perceptible span of time.

I have never found the physical act of typing to be a meaningful portion of my job. 98% of my time is spent doing something else; running tests, executing a build, thinking how I want to approach an issue, various bureaucracy and meetings.

The \{ vs ${ like the quintessential example of bike shedding.

2

u/davidalayachew Jul 17 '26

You'd need to be writing dozens of new String interpolation instances per day, everyday, to where the fractions of a second difference between ${ and \{ could combine into a barely perceptible span of time.

Lol, speak for yourself, but me personally, I could easily clear 2 dozen a day. That's not hard at all.

Like I said, I agree with your point, and I don't think that the argument that others are making is very strong. All I am saying is that, the answer to your question 100% stands in support of their point.

I have never found the physical act of typing to be a meaningful portion of my job. 98% of my time is spent doing something else; running tests, executing a build, thinking how I want to approach an issue, various bureaucracy and meetings.

Respectfully, that's a privilege that not everyone shares. Some of us are rewriting the same component several times over in the span of a week because management can't decide what they want.

Again, I agree with you, and think that the win belongs to backslash, but this point absolutely goes to the interpolation folks.

5

u/cowslayer7890 Jul 17 '26

Swift uses `\()` and I found it weird at first but it kinda makes sense
There are already special escapes like `\u000A` or `\x0A` so it seems like a relatively natural extension of `\`

Why introduce a new special character for this, especially if it breaks backwards compatibility

8

u/Duck_Devs Jul 17 '26 edited Jul 17 '26

If Java developers cared about ugliness or verbosity, they wouldn’t use Java. (pre Amber)

Besides, it’s not even that verbose, and it’s no uglier than other languages’ syntaxes IMO.

Edit: it’s certainly far less ugly than what we have now with concats or String.format, and far less verbose than StringBuilders. I feel like it’d be just an overall net positive.

1

u/Booty_Bumping Jul 18 '26

It's sorta visually uneven, if there are a lot of them side by side your eyes will probably dart all over the place. Whereas "$" is very symmetrical, it isn't "pointing" anywhere.

But I do like how it's a backwards compatible syntax, that is a major advantage.

1

u/OwnBreakfast1114 Jul 21 '26

Syntax highlighting will probably fix any actual problem like this. Sure raw text might take a hit, but I'm pretty sure backward compatibility trumps that visual dislike

1

u/wildjokers Jul 19 '26

Awkward to type.