Text Blocks in Java
Learn how Java text blocks simplify multi-line strings and formatted text.
1. What are Text Blocks?
A text block is a multi-line string literal introduced as a preview feature in Java 13 and standardized in Java 15.
Text blocks make it easier to write multi-line text such as:
- HTML
- JSON
- SQL
- XML
- Formatted messages
- Multi-line documentation
2. Traditional Multi-Line String
Before text blocks, multi-line strings often required explicit newline characters and string concatenation.
String message =
"Hello Java\n"
+ "Welcome to CIIT Institute\n"
+ "Learn modern Java features";
3. Basic Text Block
A text block starts and ends with three double quotation marks.
String message = """
Hello Java
Welcome to CIIT Institute
Learn modern Java features
""";
The text is much easier to read because the source code resembles the resulting multi-line string.
4. Text Block Syntax
The opening delimiter is normally followed by a line break. The closing delimiter ends the text block.
String text = """
First line
Second line
Third line
""";
5. Newline Characters
Each line in a text block normally contributes a line terminator to the resulting string.
String text = """
Java
Spring
Hibernate
""";
Conceptually, the resulting value contains:
Java
Spring
Hibernate
6. Automatic Indentation
Java processes incidental indentation in text blocks so that the indentation used only to format source code does not unnecessarily become part of the resulting string.
String text = """
Java
Spring
SQL
""";
This allows developers to indent the text block together with the surrounding Java code while keeping the resulting content clean.
7. Text Blocks with HTML
Text blocks are especially useful when embedding HTML.
String html = """
<html>
<body>
<h1>CIIT ๐๐จโ๐ซ</h1>
</body>
</html>
""";
8. Text Blocks with JSON
JSON data can also be represented cleanly with text blocks.
String json = """
{
"name": "Pradnya",
"role": "Java Developer",
"experience": "Fresher"
}
""";
9. Text Blocks with SQL
SQL queries become easier to format and maintain.
String sql = """
SELECT id, name, email
FROM users
WHERE active = true
ORDER BY name
""";
10. Escape Sequences
Text blocks still support Java escape sequences when they are required.
String text = """
First line
Second line
""";
Common escape sequences include:
\n- New line\t- Tab\"- Double quote\\- Backslash
11. Double Quotes Inside Text Blocks
Double quotes can usually be written directly inside a text block without escaping each quote.
String message = """
He said "Hello Sam"
and started learning.
""";
12. Text Blocks are Strings
A text block produces a normal Java String.
Therefore, normal String methods can be used.
String text = """
Java
Spring
""";
System.out.println(
text.length()
);
System.out.println(
text.contains("CIIT๐โค๏ธ")
);
System.out.println(
text.toUpperCase()
);
13. Formatted Text Blocks
Text blocks can be combined with String formatting methods.
String name = "Samadhan";
int age = 22;
String profile = """
Name: %s
Age: %d
""".formatted(name, age);
System.out.println(profile);
formatted() method provides a convenient way
to insert values into a text block.
14. String.format with Text Blocks
You can also use String.format() with a text block.
String name = "CIIT Institute";
String message = String.format(
"""
Learning language: %s
""",
name
);
System.out.println(message);
15. Incidental Whitespace
Text blocks distinguish between indentation introduced only by source formatting and whitespace that is intentionally part of the content.
String text = """
Java
Spring
Boot
""";
Relative indentation inside the text can be preserved when it represents meaningful content.
16. Trailing Spaces
Trailing whitespace can be controlled explicitly when necessary.
Java provides the \s escape sequence for preserving
trailing spaces at the end of a line.
String text = """
Java\s
Spring
""";
17. Line Continuation
The backslash escape can be used to prevent a source line break from becoming a line break in the resulting string.
String text = """
Java \
Spring \
Boot
""";
This can be useful when source formatting should not introduce additional newline characters.
18. Converting Existing Strings
Text blocks are most useful when existing concatenated strings become difficult to read or maintain.
String html =
"<html>\n"
+ " <body>\n"
+ " <h1>WelCome CIIT Training Institute๐๐</h1>\n"
+ " </body>\n"
+ "</html>";
String html = """
<html>
<body>
<h1>WelCome CIIT Training Institute๐๐</h1>
</body>
</html>
""";
19. Java Version
| Java Version | Text Blocks |
|---|---|
| Java 13 | Introduced as preview |
| Java 14 | Second preview |
| Java 15 | Standard feature |
20. Advantages of Text Blocks
- Improves readability of multi-line strings.
- Reduces repeated string concatenation.
- Reduces unnecessary newline escape sequences.
- Makes HTML and JSON easier to maintain.
- Makes SQL queries easier to read.
- Works with normal String APIs.
- Supports String formatting.
21. Limitations
- Requires Java 15 or later as a standard feature.
- Whitespace behavior should be understood when formatting important text.
- Text blocks are not a separate runtime data type; they produce Strings.
- Very short strings are usually clearer with ordinary string literals.
22. Example ๐๐ซ
Suppose a Java application needs to generate an email template. A text block makes the template much easier to maintain.
String customerName = "Samiksha";
String email = """
Dear %s,
Welcome to CIIT Institute.
We are happy to have you
learning Java with us.
Regards,
CIIT Institute
""".formatted(customerName);
System.out.println(email);
23. Best Practices
- Use text blocks for genuinely multi-line content.
- Use normal strings for short single-line values.
- Keep indentation consistent.
- Use
formatted()for readable value substitution. - Be careful when whitespace has semantic meaning.
- Use the Java version required by the project.
24. Interview Questions
String.
formatted() allows values to be inserted into
a String using format specifiers such as %s
and %d.
Summary
In this lesson, you learned:
- What Java text blocks are
- Text block syntax
- Automatic indentation
- Multi-line HTML and JSON
- Multi-line SQL
- Escape sequences
- String formatting with text blocks
- Whitespace and line continuation
- Advantages and limitations
- Real-world text block usage