JEP 378 – Text blocks leave preview in JDK 15
Text blocks were added to Java as a preview feature in JDK 13. With JDK 14 the feature was previewed for a second time with the addition of two more escape clauses. Now, text blocks will be a full-fledged feature in JDK 15 because the community is in agreement: text blocks are ready. Let’s take a closer look.
It’s been a year since text blocks made their first foray into the JDK. Now, with JDK 15, JEP 378 will remove the preview status of text blocks and make them a full-fledged feature of Java 15.
Text blocks in Java
JEP 378 is the third JEP to deal with text blocks in the JDK. Let’s take a little look at the history of text blocks over the last year or so.
JEP 355 introduces text blocks as preview feature
The first time text blocks came to the JDK was in Java 13 thanks to JEP 355. A text block is “a multi-line string literal that avoids the need for most escape sequences, automatically formats the string in a predictable way, and gives the developer control over the format when desired.” And when this original JEP landed, it caused some excitement among the community. Indeed, we featured an article by Tim Zöller called Java 13 – why text blocks are worth the wait.
Thanks to JEP 355, text blocks made their way into JDK 13 as a preview feature for the community to try out and provide feedback about. It became clear during this first preview that some use cases needed text blocks to undergo a few changes.
JEP 368 and some important changes
Newline characters and white space were the reasons for the changes made to text blocks in JDK 14. Users discovered that a string spanning multiple lines can prove problematic if newline characters appear in the string. Jim Laskey writes in JEP 368, “One part of a program may be more readable when strings are laid out over multiple lines, but the embedded newline characters may change the behavior of another part of the program. Accordingly, it would be helpful if the developer had precise control over where newlines appear, and, as a related matter, how much white space appears to the left and right of the “block” of text.”
SEE ALSO: Java 15’s latest news
This is why the second preview of text blocks introduced two new escape sequences:
\s. The former escape sequence explicitly suppresses the insertion of a newline character, and the latter translates to a single white space (\u0020), allowing it to be used as a buffer to prevent intentionally included white space from being removed.
You can read a more detailed look at JEP 368 here.
JEP 378 – text blocks are ready for launch
The upcoming release of Java 15 means it’s time to take a look at the feedback for preview features such as text blocks. This time around there were no red flags or use cases that resulted in changes to this feature, so Jim Laskey wrote JEP 378 proposing that text blocks leave preview. The JEP was confirmed to target JDK 15 and so text blocks will be a full-fledged feature in Java 15 when it arrives in September.