Nearby lessons
100 of 125Java - Text Blocks
- Text blocks — the first preview
- Switch expressions with yield
- String.formatted()
- The idea of preview iterations
Text Blocks is a core concept of the Java language. This lesson explains Java 13 — Text Blocks Arrive, Text Blocks — Clean Multi-line Strings and Text Blocks — Second Preview with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.
Java 13 — Text Blocks Arrive
Java 13 (September 2019) continued the switch expression journey and introduced the most loved new feature for developers: Text Blocks (as a preview). Writing multi-line strings in Java had always been painful — Java 13 fixed it.
Text Blocks — Clean Multi-line Strings
A text block is a String written over many lines using three double-quotes. No more "\n" and + everywhere:
Text Blocks — Clean Multi-line Strings
Text blocks are perfect for: SQL queries, JSON data, HTML templates, and any long text in code. The indentation is handled automatically — Java removes the common leading spaces of all lines.
Text Blocks — Clean Multi-line Strings
Text Blocks — Second Preview
Java 14 improved text blocks again (new escape sequences like \s to keep spaces and \ to break a line) before they became final in Java 15.
Text Blocks — Modern Multi-line Strings
Updated knowledge (Java 15+): writing long multi-line text was painful with quotes and + signs. Java 15 made it easy with text blocks using triple quotes:
- Text blocks (""" ... """) give clean multi-line Strings for HTML, SQL, JSON.
- Switch expressions got yield for block-style cases.
- formatted() formats a text block's placeholders.
- Text blocks became final in Java 15; switch expressions in Java 14.
- Preview iterations show how Java matures features carefully.