Text blocks, introduced in Java 13 as a preview feature and made a standard feature in Java 15, provide a way to define multi-line string literals in a more readable and concise manner. Text blocks help improve code readability, reduce the need for escape sequences, and make it easier to work with large text content, such as HTML, JSON, or SQL queries.
Text blocks are created using triple-double quotes ("""). The opening triple quotes can be followed by a new line, and the closing triple quotes should be on their own line after the text content.
String textBlock = """
This is a text block.
It can span multiple lines.
No need for concatenation or escape sequences.
""";
public class TextBlockExample {
public static void main(String[] args) {
String textBlock = """
This is a text block.
It can span multiple lines.
No need for concatenation or escape sequences.
""";
System.out.println(textBlock);
}
This is a text block.
It can span multiple lines.
No need for concatenation or escape sequences.
Text blocks make it easier to read and write multi-line strings without the need for concatenation or escape sequences.
Without Text Blocks:
String html = "< html>\n" +
" < body>\n" +
" < h1>Hello, World!< /h1>\n" +
" < /body>\n" +
"< /html>";
With Text Blocks:
String html = """
< html>
< body>
< h1>Hello, World!< /h1>
< /body>
< /html>
""";
Text blocks handle common escape sequences automatically, making the code cleaner.
Without Text Blocks:
String json = "{\n" +
" \"name\": \"John\",\n" +
" \"age\": 30\n" +
"}";
With Text Blocks:
String json = """
{
"name": "John",
"age": 30
}
""";
Text blocks are useful for defining HTML content in a readable manner.
public class HtmlExample {
public static void main(String[] args) {
String html = """
< html>
< body>
< h1>Hello, World!< /h1>
< /body>
< /html>
""";
System.out.println(html);
}
}
Text blocks are useful for defining JSON content without the need for escape sequences.
public class JsonExample {
public static void main(String[] args) {
String json = """
{
"name": "John",
"age": 30,
"city": "New York"
}
""";
System.out.println(json);
}
}
Text blocks are useful for defining multi-line SQL queries in a readable manner.
public class SqlExample {
public static void main(String[] args) {
String sql = """
SELECT id, name, age
FROM users
WHERE age > 18
ORDER BY name;
""";
System.out.println(sql);
}
}
Text blocks automatically handle common leading whitespace.
public class IndentationExample {
public static void main(String[] args) {
String indentedText = """
Line 1
Line 2
Line 3
""";
System.out.println(indentedText);
}
}
You can combine text blocks with variables using concatenation or formatted strings.
public class VariableExample {
public static void main(String[] args) {
String name = "Alice";
String message = """
Hello, %s!
Welcome to the text block example.
""".formatted(name);
System.out.println(message);
}
}
Text blocks handle common escape sequences, such as newlines and tabs.
public class EscapeSequenceExample {
public static void main(String[] args) {
String textBlock = """
This is a text block.
It can handle escape sequences like \n for newlines and \t for tabs.
""";
System.out.println(textBlock);
}
}
Text blocks can include line breaks directly within the text.
public class LineBreakExample {
public static void main(String[] args) {
String poem = """
Roses are red,
Violets are blue,
Text blocks are handy,
And so are you.
""";
System.out.println(poem);
}
}
Text blocks provide a powerful and convenient way to define multi-line string literals in Java. By leveraging text blocks, you can improve the readability and maintainability of your code, especially when working with large text content like HTML, JSON, and SQL queries. Understanding and using text blocks can significantly enhance your Java programming experience.