Java / Java 21 Coding Standards Interview Questions
What is a text block in Java 21?
A text block is a multi-line string literal delimited by triple double quotes (""") that lets a chunk of text such as JSON, SQL, or HTML be written in the source file exactly as it should appear, without escape sequences for quotes or explicit newline characters.
String json = """ { "name": "Alice", "role": "admin" } """;
The compiler strips incidental leading whitespace based on the indentation of the closing delimiter, so the block can be indented to match the surrounding code without that indentation becoming part of the string's value.
Coding standards favor text blocks over concatenated strings whenever a literal spans more than two or three lines, since concatenation with + and \n is harder to read and easy to get wrong, especially for embedded queries or templates.
More Related questions...