Indentation and Stripping
Control incidental whitespace.
Incidental vs Essential Whitespace
When you indent a text block to match your code, that indentation is incidental and should not be part of the string. Java strips it automatically, keeping only essential whitespace.
The Closing Delimiter Sets the Margin
Java looks at all content lines and the closing """ to find the common leftmost non-blank column. Everything to the left of that margin is removed.
public class Main {
public static void main(String[] args) {
String text = """
Hello
World
""";
System.out.println("[" + text + "]");
}
}All lessons in this course
- Declaring Text Blocks
- Indentation and Stripping
- Escapes in Text Blocks
- Use Cases: JSON and SQL