Package java.lang.template
String
but is not limited to just
Strings
.
Java string templates look like string literals or text blocks except they contain
one or more embedded expressions bracketed by \{
and }
.
An embedded expression is usually positioned in the string where the value of that
embedded expression might expect to be inserted.
String interpolation is the most general use of string templates. The
standard StringTemplate.STR
PREVIEW template processor is statically
imported into every Java compilation unit to facilitate the common use of string
interpolation.
int x = 10;
int y = 20;
String s = STR."The result of adding \{x} and \{y} is \{x + y}.";
s
in the above example will be
"The result of adding 10 and 20 is 30."
.
The expression STR."The result of adding \{x} and \{y} is \{x + y}."
above is an example of a process template expression. A process template
expression consists of a processor expression and a processor
argument separated by a dot (period). A processor expression evaluates
to an instance of type ValidatingProcessor
PREVIEW. A
processor argument is a string template that is represented by an instance of
StringTemplate
PREVIEW. The end result of the process template
expression is the value that is produced by invoking the processor's
ValidatingProcessor.process(StringTemplate)
PREVIEW
method of with the processor argument. Improper processor expressions or
improper processor arguments result in compilation errors.
In the example above, STR
is the processor that implements string
interpolation with it
ValidatingProcessor.process(StringTemplate)
PREVIEW method.
The string template in the example, represented by a
StringTemplate
PREVIEW, contains the string fragments and
embedded expression values expressed in
"The result of adding \{x} and \{y} is \{x + y}."
.
In the example, the fragments are "The result of adding "
, " and "
,
" is "
and "."
. The values are 10
, 20
and 30
,
which are the result of evaluating x
, y
and x + y
.
See StringTemplate
PREVIEW for examples and details.
String literals and text blocks can be used as proper processor arguments as
well. This is automatically facilitated by the Java compiler converting the
strings to StringTemplate
PREVIEW using the
StringTemplate.of(String)
PREVIEW method.
Users can create their own template processors by implementing one of
ValidatingProcessor
PREVIEW,
TemplateProcessor
PREVIEW or
StringProcessor
PREVIEW interfaces.
For more examples and details see StringTemplate
PREVIEW and
ValidatingProcessor
PREVIEW.
- See Java Language Specification:
-
15.8.6 Process Template Expressions
- Since:
- 21
- See Also:
-
InterfacesClassDescriptionPreview.Policies using this additional interface have the flexibility to specialize the composition of the templated string by returning a customized
MethodHandle
fromlinkage
PREVIEW.Preview.This interface is used to implement template processors that only produceString
results.Preview.StringTemplate
PREVIEW is the run-time representation of a string template or text block template in a template expression.Preview.This interface is used to implement template processors that do not throw checked exceptions.Preview.This interface describes the methods provided by a generalized string template processor.