Java SE 15: Text Blocks

The “Text Block” is a multiple-line string literal. It avoids the need to for most escape sequences. It automatically formats the string literal in an expected way and provides a control over the format on demand.

The goals of the Text Blocks (source: JEP-378):

  • simplification of expressing a strings that span over several lines with avoiding escape sequence in common case
  • enhance the string readability that denote code written in non-Java languages
  • support migration from string literals by stipulating that any new construct can express the same set of strings as a string literal, interpret the same escape sequences, and be manipulated in the same ways as a string literal
  • Add escape sequences for managing explicit white space and newline control.

The Text Blocks motivation

Sometimes is required to add non-Java language snippets inside the Java code base in order to communicate with other APIs. Such snippets may contain JSON, SQL, XML, JavaScript sequences in a string literal. This may require some effort in editing, reading and also maintaining such code. Java Text Blocks address such issues.

To define the Java “Text Block” inside the code it is required to start with three double quote characters (“””) without the zero whitespaces between, opening delimiter – (“””). The closing delimiter is exactly similar to opening one (“””) . Remember white spaces are removed automatically:

usage:
String htmlLiteral = "<html>\n" +
        "    <body>\n" +
        "        <p>Hello, HTML!</p>\n" +
        "    </body>\n" +
        "</html>\n";

String htmlTextBlock = """
      <html>
          <body>   
              <p>Hello, HTML!</p>
          </body>
      </html>
      """;

System.out.println("htmlLiteral:\n" + htmlLiteral);
System.out.println();
System.out.println("htmlTextBlock:\n" + htmlTextBlock);

// Notice: when whitespaces are added into the htmlLiteral variable equality does not hold
System.out.println("EQUAL: " + htmlLiteral.equals(htmlTextBlock));
// Notice: Text Block provides a stable predictable formatting 

output:
htmlLiteral:
<html>
    <body>
        <p>Hello, HTML!</p>
    </body>
</html>


htmlTextBlock:
<html>
    <body>
        <p>Hello, HTML!</p>
    </body>
</html>

EQUALS: true
//After adding more white spaces into the htmlLiteral then 
EQUALS: false 

Another additional benefit of the “Text Block” is a gained formatting control. The line-terminator\” can be used to suppress adding a newline character. Inside the “Text Block” the method “replace” and newly introduced method “formatted” can be use to avoid unnecessary and less readable “Text Blocks” concatenation:

usage:
String helloText = "Hello, JavaScript!";
String javaScriptTextBlockReplace = """
        function helloJavaScript() { \
            print('$helloText'); \
        } \
        helloJavaScript(); \
        """.replace("$helloText", helloText);
String javaScriptTextBlockFormatted = """
        function helloJavaScript() {
            print('%s');
        }
        helloJavaScript();
        """.formatted(helloText);

System.out.println("javaScriptTextBlockReplace:\n" + javaScriptTextBlockReplace);
System.out.println();
System.out.println("javaScriptTextBlockFormatted:\n" + javaScriptTextBlockFormatted);

output:
javaScriptTextBlockReplace:
function helloJavaScript() {     print('Hello, JavaScript!'); } helloJavaScript(); 

javaScriptTextBlockFormatted:
function helloJavaScript() {
    print('Hello, JavaScript!');
}
helloJavaScript();
 
Main: Java tutorials