I updated my JDK to JDK 23 and now get a weak warning for any String concatenations like System.out.print("The capital city of " + index + " is ");
saying I should use a String template, but when I apply the IDE quick fix to apply the template, the java.lang.StringTemplate
class doesn’t exist which I can confirm even after manually looking for the StringTemplate
in the JDK 23 library.
Is anyone else missing this class or is it probably an issue with my JDK?
The exact warning message is:
String can be replaced with template
5
The string template was a preview feature in Java 21 and Java 22, and has been withdrawn for further redesign. In other words, this feature never existed in mainline Java (you needed to enable preview features to use it), and now with the release of Java 23, it doesn’t exist at all.
If you get recommendations from IntelliJ to use string templates, you either need to update IntelliJ (it shouldn’t suggest this at all for Java 23), or possibly you have set your language level to “21 (Preview)” or “22 (Preview)”, instead of normal (non-preview) language level “21”, “22” or “23”. In other words, go to File, Project Structure, and on Project check the language level, and double-check for individual Modules (on their “Sources” tab).
As an aside, recent Java versions already optimize string concatenation like "The capital city of " + index + " is "
pretty well by replacing it with a low-level form of templating in the compiled bytecode.
2
String Templates were a preview feature of Java 21/22 and were removed in Java 23. The reason given was:
After some experience with the prototype, it became clear that the processor-centric nature of the proposed design was confusing to users and lacked the desired compositionality.
Earlier versions of IntelliJ (2024.1?) were not yet fully aware of all the features of Java 23. As of recent versions (2024.2.2 or possibly earlier) it is now fully compatible with Java 23 and will no longer suggest string templates.
2