0Pricing
Java Academy · Lesson

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

  1. Declaring Text Blocks
  2. Indentation and Stripping
  3. Escapes in Text Blocks
  4. Use Cases: JSON and SQL
← Back to Java Academy