What are text blocks in Java?
Why Interviewers Ask This
Senior Java engineers are expected to reason about architecture, performance, and edge cases. This question separates mid-level from senior candidates by testing deep system-level understanding.
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.
Pro Tip
Demonstrate both theoretical understanding and practical experience. Say what it is, then give an example of how you actually used it in a Java codebase.