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

Question of the Week #128
What are text blocks and how can they be used?
5 Replies
Eric McIntyre
Eric McIntyre5mo ago
String literals are enclosed with double quotes.
String someString = "Hello World";
System.out.println(someString);
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);
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);
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);
String whiteSpaceExample = """
Hello
World
:)""";
System.out.println(whiteSpaceExample);
The above code outputs the following text:
Hello
World
:)
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.
Eric McIntyre
Eric McIntyre5mo ago
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
String noLineBreak = """
Hello \
World\
""";
System.out.println(noLineBreak);//Hello World
📖 Sample answer from dan1st
Eric McIntyre
Eric McIntyre5mo ago
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
Eric McIntyre
Eric McIntyre5mo ago
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";
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>
""";
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" +
"}";
String json = "{\n" +
" \"name\": \"John Doe\",\n" +
" \"age\": 30\n" +
"}";
Eric McIntyre
Eric McIntyre5mo ago
With Text Blocks:
String json = """
{
"name": "John Doe",
"age": 30
}
""";
String json = """
{
"name": "John Doe",
"age": 30
}
""";
Key characteristics of text-blocks: - Indentation Management: The compiler looks at all the lines in your text block. It figures out the smallest amount of space that every non-empty line starts with. This "common" leading whitespace is then removed from every line. It also gets rid of any spaces at the very end of each line. - Trailing Whitespace: Trailing whitespace on each line is generally removed. To preserve trailing whitespace, you can use the escape sequence \s. - Line Terminators: Line breaks within the text block are normalized to \n in the resulting string. - Escaping: Most escape sequences like \n or \t are not needed. However, to include """ within the text block itself, you can escape one of the quotes, for example, \""".
⭐ Submission from daysling

Did you find this page helpful?