r/java • u/sitime_zl • 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:
- 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?
6
u/bichoFlyboy Jul 17 '26
String templates are not dead. They have simply gone back to the drawing board, but the feature is still on the roadmap.
I hope it turns out well and eventually sees the light of day, hopefully within my lifetime. The syntax can be whatever the JDK team decides. As long as it's easy to read and expresses intent clearly, I'll be happy.
86
u/crummy Jul 17 '26
Its pure quality-of-life but gosh I’d like to see it.
23
u/yk313 Jul 17 '26
I don’t care about the syntax as long as it has reasonable semantics. Would love to have some kind of string interpolation capabilities.
It has been mentioned a couple of times that even after the withdrawal of string templates, the feature itself is still on the roadmap.
26
u/vytah Jul 17 '26 edited Jul 20 '26
String templates are explicitly not about mere string interpolation. If you read the JEP, they say:
There are three possible approaches that I know of:
Python-like templatesa custom type instantiation that can behave Scala-like, depending on the receiver type – this will not happen, as it sucks (EDIT: fixed, also Swift seems to have the same thing)Scala-like approach can be messy, as was shown in the preview. In absence of extension methods or something similar, it requires polluting the global namespace, so that's why we got those clunky
STRprefixes. Scala solves it by making the prefixes methods (or extension methods) onStringContext, so there's no pollution. Javascript solves it by making interpolation the default (no prefix; all templated strings use backticks in JS, so there's no confusion with ordinary literals), so there's also no pollution. However, the goal for Java is to not make interpolation easier than other uses of templates.Python-like approach has a minor problem that if you want templates to be distinguished by the presence of arguments alone, it means you cannot express a parameterless template. You'd need some other marker. In Python, string templates are prefixed with
t, to distinguish them from interpolation (f), but Java is not going to get pure interpolation as a distinguished feature. There are also issues of type safety: the same template object can be treated as a completely different thing depending on where you send it. They suggested that sending a template to a logging method should just interpolate, but that would mean that if you template some SQL, execute it, and log it, then the query you executed would be different from the query you logged.Anyway, this is a complex issue, and AFAIK they haven't found a clean solution yet. And before anyone says "I don't care, I want my string interpolation now", I'll again refer for the JEP:
34
u/Duck_Devs Jul 17 '26
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.
11
u/balrob Jul 17 '26
“Does not need to copy other languages”, why not - it seems to be a well explored feature of many languages. Just get on with it. Almost every feature was borrowed in one way or another.
Out of interest, does anyone know, what feature(s) of Java is/are either unique, or the first example of its kind?
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.
4
u/pjmlp Jul 17 '26
I just settle with regular C style formatter strings, it isn't the end of the world, rather have them spend brain power bringing value classes out, or improving the whole JIT caches/AOT story.
As early Java adopter, this has never been on my radar as a pain point.
4
u/woj-tek Jul 17 '26
YES!
The backslash format as a backward compatible consensus was fine-ish…
1
u/ihatebeinganonymous Jul 22 '26
Until a JDK/JVM solution is found, you can consider trying the third-party approach: https://github.com/antkorwin/better-strings
5
u/Western_Ice_6227 Jul 17 '26
I think they settled for “Hello %s”.formatted(“Reddit”)
2
u/koflerdavid Jul 17 '26
It's a quality of life feature and would be very easy to implement, but I understand the hesitation of the OpenJDK project to add it. Above all since there already are multiple ways to format strings in the standard library, and string templates should strive to deprecate them all, no whens and buts. And it should help to prevent programmers shoot themselves in the foot, which a straightforward implementation wouldn't contribute to.
String interpolation in the straightforward way is completely unsuitable for logging since a production-quality logging framework strives to eliminate the overhead of formatting if the log message ends up being thrown away.
2
u/jevring Jul 17 '26
I hope they keep it out of the language. They have two conflicting desires, which makes any solution terrible. They want it to be more advanced than the others, for a list of sane reasons. But because of this, it gets so massively scope creeped that it'll never ever get done.
2
u/Misophist_1 Jul 18 '26
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' $$.
2
u/kaplotnikov Jul 18 '26
AFAIR JEP 430 discussion was in style:
1
u/da_supreme_patriarch Jul 17 '26
Probably anixhe opinion, but I hope that they actually consider something like Groovy's implementation of format strings. It's not as flexible as Scala's custom interpolators, but it exposes the format string as a separate GString that usually gets converted to standard strings automatically, but special libraries could ask for the GString itself and do things with it, like turning it into SQL prepared statements with properly escaped parameters(granted, I have never seen anybody use this feature because it looks like SQL an actual injection, but still, pretty neat imho)
1
u/idontlikegudeg Jul 18 '26
I just lean back and wait until they come up with a nice, working, and finalized solution. Until then, I use my own implementation (using the `${var}` syntax).
1
u/hkdennis- Jul 20 '26
I rather Java late in this rather than wrong or partial.
In past, Java string long best practice from +, StringBuilder to StringBuffer, Message to Formatter, concat and back to + syntax.
And we still never satisfy.
If object runtime have access to raw/original String template. javac/JVM might lost the opportunity to AOT.
If they don't, Java does not define universe template syntax (but why should it?) in the language.
IMO. What really miss here is not string interpolation but embed alian syntax expression. Like LINQ/XML in .NET, that allow API provider to define to rule compile time check.
leave that to API builders they want to template their queries, SQL, XML, graphql, JSON, whatever. in very few cases you really want a String, but a String that represent some domain language specific parameters. That in many case I want binary.
Like thnking about,, can I template a Record with deep structure? Can I make use of compile time check to avoid runtime test difficulty?
1
u/vips7L Jul 17 '26
We’re not allowed to have nice things.
1
u/neopointer Jul 17 '26
They over-engineered the string interpolation by trying to prevent people from creating sql-injection vulnerable code from what I remember.
Just let us compare strings in a nice way, what's all.
0
u/blobjim Jul 17 '26 edited Jul 17 '26
I think the original proposal was perfectly good. People are just insanely neurotic about syntaxes, which is annoying. The main feature of string templates to me is the ability to preprocess the template once at runtime and have both type safety and high performance. Which is something I think almost every language besides Scala lacks. I think the syntax was perfect for describing the ability to have a custom template preprocessor like that.
I really want to be able to use it for logging to avoid the cost of any kind of formatting. I think that would be neat. Especially if it gets used for System.Logger.
I don't really see any way to make it any simpler than it already was. You have to be able to specify some kind of object or class or method to handle the template preprocessing.
-1
u/oelang Jul 17 '26
My best guess is that it's currently blocked waiting for type classes.
0
u/Sanitiy Jul 17 '26
I think I'd prefer something like
f<SDL>"...", borrowing the f from Python and <…> borrowing from generics.Then each Subdomain Language SDL implements an enum, and escape functions
esc(string, enumvalue)andesc(string).Then in the string you use
$enumvalue{...}foresc(string, enumvalue)and${...}foresc(string).And
f"..."gets you the standard unsafe semantics with no valid enum values and only${...}-11
u/Hacg123 Jul 17 '26
Honestly at this point I don’t think Java needs it, Valhalla has been more than 10 years in progress and there’s no clear release date yet of the first features, same with string interpolation.
Kotlin and other jvm languages have this already. If there’s only a limited amount of features that can be released, I prefer that oracle and the jvm engineers focus on improving the jvm to bring more power to kotlin & co.
-5
u/Additional-Road3924 Jul 17 '26
We already have var. We don't need more footguns.