Java Text Blocks
Learn more about the latest release from Oracle: text blocks.
Join the DZone community and get the full member experience.Join For Free
In the post on May 13th: "RFR: Multi-line String Literal (Preview) JEP [EG Draft]" on the OpenJDK amber-spec-experts mailing list, Jim Laskey announced a draft feature JEP named "Text Blocks (Preview)" (JDK-8222530).
Laskey's post opens with (I've added the links), "After some significant tweaks, reopening the JEP for review" and he is referring to the draft JEP that was started after the closing/withdrawing of JEP 326 ["Raw String Literals (Preview)"] (JDK-8196004). Laskey explains the most recent change to the draft JEP, "The most significant change is the renaming to Text Blocks (I'm sure it will evolve over time Text Literals or just Texts.) This is primarily to reflect the two-dimensionality of the new literal, whereas String literals are one-dimensional." This post-"raw string literals" draft JEP previously referred to "multi-line string literals" and now refers to "text blocks."
The draft JEP "Text Blocks (Preview)" provides a detailed overview of the proposed preview feature. Its "Summary" section states:
Add text blocks to the Java language. A text block is a multi-line string literal that avoids the need for most escape sequences, automatically formats the string in predictable ways, and gives the developer control over format when desired. This will be a preview language feature.
This is a follow-on effort to explorations begun in JEP 326, Raw String Literals (Preview).
The draft JEP currently lists two "goals" of the JEP and I've reproduced the first two here:
- "Simplify the task of writing Java programs by making it easy to express strings that span several lines of source code, while avoiding escape sequences in common cases."
- "Enhance the readability of strings in Java programs that denote code written in non-Java languages."
The "non-goals" of this draft JEP are also interesting and the two current non-goals are reproduced here:
- "It is not a goal to define a new reference type (distinct from
java.lang.String) for the strings expressed by any new construct."
- "It is not a goal to define new operators (distinct from
+) that take
The current "description" of the draft JEP states:
A text block is a new kind of literal in the Java language. It may be used to denote a string anywhere that a string literal may be used but offers greater expressiveness and less accidental complexity.
A text block consists of zero or more content characters, enclosed by opening and closing delimiters.
The draft JEP describes the use of "fat delimiters" ("three double quote characters":
===) in the opening delimiter and closing delimiter that mark the beginning and end of a "text block." As currently proposed, the text block actually begins on the line following the line terminator of the line with the opening delimiter (which might include spaces). The content of the text block ends with the final character before the closing delimiter.
The draft JEP describes the "text block" treatment of some special characters. It states, 'The content may include
" characters directly, unlike the characters in a string literal.' It also states that
\n are "permitted, but not necessary or recommended" in a text block. There is a section of this draft JEP that shows examples of "ill-formed text blocks."
There are numerous implementation details covered in the draft JEP. These include "compile-time processing" of line terminators ("normalized" to "to LF (
\u000A)"), incidental white space (differentiation of "incidental white space from essential white space" and use of
String::indent for custom indentation management), and escape sequences ("any escape sequences in the content are interpreted" per Java Language Specification and use of
String::translateEscapes for custom escape processing).
Newly named "Java Text Blocks" look well-suited for the stated goals and the current proposal is the result of significant engineering effort. The draft JEP is approachable and worth reading for many details I did not cover here. Because this is still a draft JEP, it has not been proposed as a candidate JEP yet and has not been targeted to any specific Java release.
Published at DZone with permission of Dustin Marx, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.