Java finally supports string interpolation! I’m very happy about it. However, the syntax seems to me a bit clumsy
// string templating syntax from Java 23
String greeting = STR."Hello {name}!";
Specifically, I have doubts about the necessity of that STR
prefix which adds as many as four extra characters (counting the dot). Besides, it looks like I’m querying some unusual static property on a global STR
object which adds to the confusion
It’s clear that for the back compatibility reasons, it couldn’t be just “Hello /{name}!” as there could theoretically be a literal string “{name}” somewhere in existing Java code. However, there are solutions from other languages that seem to address that better
- Backticks, a-la JS (they could use triple backticks for text blocks). Adds zero extra characters
String greeting = `Hello, {name}!`;
- Dollar signs, a-la C#. Adds one extra character
String greeting = $"Hello, {name}!";
What forces drove Java creators to use STR.
instead?