Java Text Blocks — Free Java Tutorial
Learn Java Text Blocks in Java with a free, beginner-friendly tutorial, examples and practice for Indian students on Syllab.in.
TL;DR: Learn Java Text Blocks in Java with a free, beginner-friendly tutorial, examples and practice for Indian students on Syllab.in.
Written & reviewed by the Syllab.in Academic Team (CBSE/NCERT subject experts) · Updated
Java Text Blocks in Java
Text blocks (Java 15+) are multi-line string literals surrounded by triple double-quotes """. They preserve formatting and eliminate the need for escape sequences.
The opening """ must be followed by a newline. Indentation is automatically stripped based on the least-indented line (incidental whitespace removal).
Text blocks support the same escape sequences as regular strings. Use \n for explicit newlines, \t for tabs. String.formatted() or formatted() works the same as String.format().
Text blocks are ideal for: SQL queries, JSON/XML literals, HTML templates, and any multi-line string content.
Java Text Blocks — Syntax
// Old way
String html = "<html>\\n" +
" <body>Hello</body>\\n" +
"</html>";
// Text block
String html = """
<html>
<body>Hello</body>
</html>
""";
// With formatting
String json = """
{ "name": "%s", "age": %d }
""".fo
Learn Java Text Blocks step by step with Syllab's free interactive Java tutorial — runnable code examples, practice exercises and instant AI feedback, all free with no signup. Explore the full Java course →