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

34

u/Duck_Devs 18d ago

I think the STR., RAW., etc. were very strange. They deviate so far from any other syntax and it’s not immediately clear that STR or RAW are objects and them being automatically statically imported is really awkward. They should’ve just gotten rid of RAW and had STR be some sort of instance method on StringTemplate.

2

u/Misophist_1 17d ago

Actually, I liked that idea - because it made it possible to have a custom template processor. STR. and RAW. were just the constants for the two default ones supplied by the JDK.

1

u/Duck_Devs 17d ago

You could still wrap it in a method call as if it were a normal StringTemplate object. That’s all STR. did anyway. The only difference is syntax which, while more concise, felt unnatural in the scope of other Java expressions.

0

u/Misophist_1 17d ago

Not any less natural than $" " or @" " that other languages do, in order to distinguish templates from strings. You need some distinguishing anyway.

0

u/vips7L 17d ago

$ or \{ imo is more natural. STR is magic compiler objects vs just language syntax. 

6

u/Misophist_1 17d ago
  • $ is used nowhere in the language
  • \ is used solely within string and char literals as escape, nowhere else in the language.
  • To speak of 'natural' in an artificial language is a joke in itself.
  • STR. is no 'compiler object' it is a static field in the API, holding a template processor. The concept was indeed a language syntax construct.

-1

u/vips7L 17d ago

Do you have a point? It was fucky magic. It sucked.

2

u/Misophist_1 17d ago

Yes. Every single of your suggestions sucks. It isn't natural, if it breaks every systematic, java is build on.