

What a mission to insert all the escape characters properly! It becomes so much simpler with text blocks: String deploymentDescriptor = Here’s a longer XML example: String deploymentDescriptor = We can now replace the multiple string literals, escape sequences and string concatenation operators with a text block: String html = """ Here’s an HTML example: String html = "" + We know that we have to use escape sequences to insert special characters into string literals, such as newlines and carriage returns, as well as escaping double quotes. The string content ends at the last character before the first double quote of the closing delimiter.Īs a first example of text blocks, let’s see how they can improve the readability of embedded XML, HTML, JSON or SQL. The closing delimiter is also a sequence of three double quote characters ( """).The actual string content begins at the first character after the line terminator of the opening delimiter. The opening delimiter is a sequence of three double quote characters ( """) followed by zero or more white space characters followed by a line terminator.The compiler automatically formats the string literal and gives us additional control over this formatting if needed.Ī text block consists of zero or more content characters, enclosed by opening and closing delimiters.

What is a Java Text Block?Ī text block is a multi-line string literal that doesn’t require us to use escape sequences such as \n, \", etc. We probably won’t ever use Java 15 directly because Java 17 is currently available, and is a LTS (long term support) release. That said, most of the important preview features have become permanent in later Java versions. The feature is fully specified and implemented, but might not become permanent. In this week’s post we’re going to look at Java text blocks, which were a preview language feature in JDK 13 and JDK 14.Ī preview feature is a new feature of either the Java language, the JVM or the JSE API.
