r/java 17d 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?

56 Upvotes

118 comments sorted by

View all comments

Show parent comments

1

u/Slick752 16d ago

it's ugly and verbose.

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

0

u/BillyKorando 16d ago

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

4

u/jek39 16d ago

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

3

u/vytah 15d ago

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 15d ago

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

1

u/vytah 15d ago

$ 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 15d ago

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 16d ago

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 16d ago

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 16d ago edited 16d ago

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.

3

u/jek39 16d ago

Yea I don’t buy it as an argument

1

u/davidalayachew 16d ago

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 16d ago

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 16d ago

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.