Robust and Flexible String Templates in Java
summary
Java 21/22 ships preview string templates that embed expressions in text blocks using a backslash syntax; the JDK provides three processors (StStr, fmt, raw) and custom processors can return any type (examples: a SQL processor that builds PreparedStatement and binds parameters to produce ResultSet, and a JSON processor that returns parsed objects). The feature is preview-only, removed from OpenJDK 23 pending redesign, and current limits include no dynamic map-lookups, inability to load templates from files, and the need to sanitize inputs to avoid injection.
AI-written summary · about
chapters (24)
- 0:00Introduction to talk
- 1:49Why strings matter in Java
- 3:14What is string interpolation
- 6:30History of string handling in Java
- 7:34String templates in Java 21
- 8:17Problems with current string concatenation
- 10:02SQL injection risks with strings
- 12:38Security threats in string interpolation
- 14:46Handling injection with frameworks
- 15:08Preview feature warning
- 16:58Java release model explained
- 17:22Basic string template syntax
- 19:30Text blocks with string templates
- 20:35Built-in string processors
- 22:45How string template processing works
- 24:40Java Almanac and JDK changes
- 26:11Creating custom processors
- 29:25SQL processor example
- 32:44JSON processor example
- 34:53Benefits of custom processors
- 35:16Surprising email from Brian Goetz
- 36:18String templates future status
- 39:28Should you use string templates now
- 43:28Conclusion and benefits summary
related talks
15mString Templates - Syntactic sugar or useful improvement?
58mJava's New Powers Hero or Villain in Your Code?
45mWhat's New in Java 21
50mJava Next - From Amber to Loom, from Panama to Valhalla
47mThe SolutionFactory to Java's Problems
02h 47mDay of the Dukentacle: The Best Way to Predict the Future Is to Build It Yourself
51mJava Next - From Amber to Loom, from Panama to Valhalla
02h 31mJava 23 - Better Language, Better APIs, Better Runtime
