☕ Java Advanced

What are text blocks in Java?

Answer

Text blocks (Java 15, standard) are multi-line string literals that avoid the need for most escape sequences. Delimited by triple quotes """, they preserve the formatting of the text while stripping common leading whitespace (indentation). This makes embedding JSON, SQL, HTML, or XML in Java code much more readable: the text block """ { "name": "Alice" } """ produces a clean JSON string without manual concatenation and escaping. The closing """ position determines the indentation level stripped. Escape sequences like \n still work, and the new \ line terminator suppresses the newline. Text blocks are just String values at runtime.