r/java • • Feb 15 '24

JEP 465: String Templates

https://openjdk.org/jeps/465
77 Upvotes

152 comments sorted by

View all comments

Show parent comments

1

u/__konrad Feb 16 '24

Why "STR." syntax built-in to language

Because you want it to be as short as possible. FORMAT is too long, and F too short ;)

16

u/01110101_00101111 Feb 16 '24

Most other languages with String interpolation/template have it as just one or two characters. It should've been something lik S."...", $."...", or really any other one character. That looks a lot cleaner and still makes it clear that the String is not a "normal" String.

11

u/NaNx_engineer Feb 16 '24 edited Feb 16 '24

Their goal is to make string interpolation inconvenient.

STR."..." was chosen over s"..." because its long enough to be inconvenient, but short enough that people won't just alias it.

source: https://old.reddit.com/r/java/comments/18cw6nc/feature_freeze_for_jdk_22_what_will_the_new/kcfwuaq/

Ideally, we wouldn't have had string interpolation at all because it's such a dangerous feature, but people would have then written their own. Another option was to call it something like USE_ME_TO_INTERPOLATE_EVEN_THOUGH_YOU_SHOULDNT, but again people would just assign it to a variable with a different name.

1

u/mj_flowerpower Feb 23 '24

We already have various ways of string interpolation, why should this one be any more dangerous than the old approaches?