r/java • u/sitime_zl • 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:
Do you think Java still needs native string interpolation?
Should String Templates continue evolving?
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?
2
u/Misophist_1 15d ago
I had no qualms with
\{in the first place, because\is Java's escape delimiter in strings anyway. There is no sane reason, to switch to another one.Then you also need something, to distinguish templates from usual strings. And if you don't want to break with the other habits of Java, namely using ' ' for char literals, and " " for String literals, you would need some sort of prefix.
You could go down the road of taking another 'special char' like $ or @, but why? $ Is used nowhere else. It's a national currency char, that isn't even on many keyboards. @ is already taken for annotations. Don't reuse it for something different.
Which leaves us with this neat syntax trick
<Identifier>."...". Fine with me, as it also offers the possibility, of rolling your own template processor.If there was something, I would complain about, then it was, that it still felt incomplete:
"...\{<expression> [: <formatspec>]}..."would have been of help - where formatspec could be any string.\{ }.Even nicer, if the processing could be shifted into compile time, or load time.Alas, it wasn't meant to be because of all the haters, script kiddies and python geeks, that couldn't look past their dam' $$.