#Week 128 — What are text blocks and how can they be used?

1 messages · Page 1 of 1 (latest)

torpid prairieBOT
#
Question of the Week #128

What are text blocks and how can they be used?

harsh hamletBOT
#

String literals are enclosed with double quotes.

String someString = "Hello World";
System.out.println(someString);

These "normal" String literals can only span a single line. If a String consisting of multiple lines is needed, it is possible to use the \n escape sequence:

String stringWithMultipleLines = "Hello\nWorld";
System.out.println(stringWithMultipleLines);

An alternative to this is to use text blocks which have been introduced in JDK 15 (first preview in JDK 13). A text block starts and ends with """ and allows line breaks in between:

String textBlock = """
  Hello
  World""";
System.out.println(stringWithMultipleLines);
#

To ensure leading and trailing space is properly handled, Java differentiates between essential whitespace and incidential whitespace. Incential whitespace is all leading whitespace in the text block that is present in all lines. If two lines in the same text block have a different amount of leading whitespace, only the minimum amount is considered incidential whitespace. Incidential whitespace is not included in the resulting String. On the other hand, all other (leading) whitespace is considered essential whitespace and included in the String.

String whiteSpaceExample = """
  Hello
    World
   :)""";
System.out.println(whiteSpaceExample);

The above code outputs the following text:

Hello
  World
 :)

The two spaces that are present in all 3 lines are considered incidential whitespace and not included. The two remaining spaces before World and the one remaining whitespace before :) are considered essential whitespace and hence included.

#

Text blocks can also include line breaks that are not included in the resulting String. This can be done by adding a \ at the end of the line before the line break.

String noLineBreak = """
  Hello \
  World\
  """;
System.out.println(noLineBreak);//Hello World
📖 Sample answer from dan1st
#

Text blocks are a Java language feature that allows to use multiline string embedded in your source code. To use this feature you just open three double quotes and a new line to start the block, put the multiline text in the next line and close three double quotes to end the text block.

Example:
String myTextBlock = """
This is the first line.
This is the second line.""";

Submission from maicosmaniotto
#

Text blocks were introduced as a preview feature in Java 13, as a feature designed to make working with multiline string literals easier, and they've became a standard feature since Java 15.

The primary goal of text blocks is to simplify the creation of strings that consists of multiple lines and escape sequences, such as snippets of JSON, XML, HTML, or SQL, by reducing string concatenations and escaping in general.

Syntax:
A text block starts with three double-quote characters """ followed immediately by a newline. The content of the string follows on subsequent lines, and the text block is closed by another three double-quote characters """.

Example:

HTML:

Traditional Muti-line String:

String html = "<html>\n" +
              "    <head>\n" +
              "        <title>My Page</title>\n" +
              "    </head>\n" +
              "    <body>\n" +
              "        <h1>Hello, World!</h1>\n" +
              "    </body>\n" +
              "</html>\n";

With Text Blocks:

String html = """
              <html>
                  <head>
                      <title>My Page</title>
                  </head>
                  <body>
                      <h1>Hello, World!</h1>
                  </body>
              </html>
              """;

JSON Example:
Traditional Mutli-line String:

String json = "{\n" +
              "    \"name\": \"John Doe\",\n" +
              "    \"age\": 30\n" +
              "}";