Since I am using Java 14 and 15 preview features. Trying to find the string interpolation in java.
The closest answer I found is
String.format("u1=%s;u2=%s;u3=%s;u4=%s;", u1, u2, u3, u4)
Since the answer which I got from lots fo references are old answers asked 4,5 years ago. Is there any update on the string interpolation in java 11,12,13,14,15 equivalent to C#
string name = "Horace";
int age = 34;
Console.WriteLine($"Your name is {name} and your age {age}");
EDIT: JEP 430, which covers this and much more, has been proposed to target JDK 21.
String::formatted
aids in situations where interpolation might be desired." – Millworkinstanceof
feature (avoid those redundant looking casts). It might seem like something like this is such a small thing to add, but it takes months/years to work out all the details, maybe to eventually decide that a feature should not be added after all. String interpolation in particular is just not at the top of the TODO list currently. – Fusionism$"Your name is {name} and your age {age}"
instead of"Your name is "+name+" and your age "+age
– Craniometer$"..."
is definitely more readable & less bug-prone than".." + ".."
, let aloneString.format("..", name, age)
. +Interpolation is jut one of many – Stake{
or}
characters (or what I need to do when I want to insert literal{
or}
characters) or identifiers that do not match variables or whether the compiler or the runtime is supposed to resolve those identifiers. For"constant"+variable+"…"
, it’s clear that I get response at compile time (or immediately while typing when using an IDE). Can’t agree that Java doesn't prioritize productivity, I mean, name the last Java version that didn’t introduce language improvements in that regard… – Craniometer+
. It was more aboutString.format
vs native string interpolation (you know what I mean). And I don't want my remarks to be construed as radical; Java is getting positive changes and I know that designers are good at picking the right features... Yet that doesn't change the fact that devs would have a very long wish-list of nice-to-haves. And I don't think it's merely because of low priority, but because of conflict (current and future backward compatibility, etc.) – Stake"Age: $age."
is so much better than the proposed"Age: {age}."
, my opinion does not change. – Craniometer$"Age: {age}."
is more productive than"Age: " + age + "."
. Even with just 1 variable it is shorter and sweeter. Now imagine strings where you add 8, 10, 15(!) variables into it. Java would benefit from implementing string interpolation like C# (and other langauges) has. – Canotas$"Your name is {name} and your age {age}"
vs"Your name is "+name+" and your age "+age
, there is no difference at all. That feature will never compensate the productivity you already lost while writing such comments. – Craniometer:"+text+".
would be formatted to:" + text + ".
. So there are your extra characters. String interpolation like C# has should be added to Java because it has the same result, with less code without hurting readability. In fact people would argue that it is even more readable but that would perhaps be too subjective. Again, your example has 1 variable. But what about strings with e.g. 5 variables? Do you still not see how it would be more productive? – Canotas`
is not currently used in the language. – Myelitis@
me, so I didn’t notice your response. My answer today would be the same as back then, regarding the savings, even considering additional white-space and more variables. In fact, it looks weird to me, to configure formatters to insert additional white-space for readability, followed by arguing that the alternative, denser syntax is more readable. However, seeing what they’ll do with JEP 430, I changed my mind. String interpolation is indeed a useful thing, but not because you might save typing. Solving the operator precedence issue, allow sanitizing arguments, etc… – Craniometer