Sub topic 8
How Indentation Is Stripped
Assume you know Java variables, loops, methods, and ordinary String literals. This chapter also assumes Java 15 or later, because text blocks became standard in Java 15.
A text block is a multi-line String literal. Its opening delimiter is three double quotes:
"""The compiler removes some indentation from the text block. This indentation is called incidental whitespace. The goal is to let your Java source stay indented while keeping the resulting string readable.
The Common Indentation
Java looks at the leading whitespace on each non-blank line. It finds the smallest common indentation and removes that amount from every line.
In this example, the content lines and the closing delimiter have eight spaces of indentation. The beta line has two additional spaces:
public class Main {
public static void main(String[] args) {
String text = """
alpha
beta
""";
System.out.print(text.replace(" ", "·")
.replace("\n", "↵\n"));
}
}The output is:
alpha↵
··beta↵The eight common spaces were removed. The two extra spaces before beta remain. The closing delimiter appears on its own line, so the resulting string also ends with a newline.
The indentation of the Java statement itself does not matter. Only the whitespace inside the text block and the position of the closing delimiter affect the result.
The Closing Delimiter Sets the Margin
The closing delimiter is the final """. Its position helps define the left margin of the text block.
When the closing delimiter is aligned with the content, common indentation is removed:
String aligned = """
alpha
beta
""";The value of aligned is equivalent to:
alpha
betaIf the closing delimiter starts at the left margin, no common indentation is removed from the content:
String preserved = """
alpha
beta
""";The value of preserved contains two spaces before both words:
alpha
betaA closing delimiter at the left margin is useful when leading spaces are part of the data. It is usually less convenient for ordinary messages because the source indentation and the resulting indentation become harder to compare.
Blank lines do not usually determine the common indentation. For example, a blank line containing spaces does not force every other line to retain those spaces. The non-blank lines determine the indentation to remove.
Trailing Spaces and `\s`
Indentation at the beginning of a line is different from spaces at the end of a line. Trailing spaces are easy to miss in source code and can be removed during text-block processing.
Use the \s escape when a line must end with a space. The escape is converted to one space after indentation processing:
public class Main {
public static void main(String[] args) {
String text = """
name:\s
value
""";
System.out.print(text.replace(" ", "·")
.replace("\n", "↵\n"));
}
}The first line ends with a visible space:
name:·↵
value↵This distinction matters when generating fixed-width output, formatted reports, or data where spaces have meaning. Use trailing spaces sparingly because they are difficult for readers to see and maintain.
Predicting the Result
To predict a text block’s exact value:
- Ignore the opening delimiter’s line.
- Identify the leading whitespace on each non-blank content line.
- Include the closing delimiter’s indentation when determining the left margin.
- Remove the common indentation.
- Keep any additional indentation.
- Remember that the closing delimiter normally leaves a final newline.
- Apply escapes such as
\safter indentation has been processed.
Exercise
What characters does this text block contain? Show spaces as · and the newline as ↵.
String text = """
one
two
""";
Show answer
The value is one↵\n··two↵\n. Four common spaces are removed, while the two extra spaces before two remain.