Text Blocks in Java
Writing a letter, a block of JSON or an SQL query as a normal string means a mess of "..." + "\n" + on every line, and escaped quotes everywhere. A text block (standard since Java 15) lets you write multi-line text the way it looks: between triple quotes, with real line breaks and plain " characters. Java even removes the indentation you used to line it up with your code.
A Letter as a Text Block
public class Letter {
public static void main(String[] args) {
String letter = """
Dear Advitha,
Your order #301 has shipped.
Thanks,
"PoolString Store"
""";
System.out.print(letter);
}
}Dear Advitha,
Your order #301 has shipped.
Thanks,
"PoolString Store"How it works
- A text block starts with
"""followed by a new line, and ends with""". - Line breaks in the source become real line breaks (
\n) in the string. - Quotes inside need no escaping:
"PoolString Store"is written as is. - The extra spaces used to indent the block inside the method are removed, but the deliberate 2-space indent on the second line is kept.
How the Indentation Is Removed
Java looks at all the lines, including the line with the closing """, finds the smallest indentation, and removes exactly that much from every line. So the position of the closing quotes controls the result:
Closing """ placed... | Effect |
|---|---|
| Lined up with the text | No leading spaces remain |
| Further left than the text | Every line keeps the difference as leading spaces |
At the end of the last text line (Thanks""") | No newline after the last line |
Escapes You Still Have
| Escape | Meaning |
|---|---|
\ at the end of a line | Join with the next line (no line break) |
\s | A single space that won't be stripped from the end of a line |
\""" | Three quotes inside the block |
\n, \t | Still work if you need them |
public class Join {
public static void main(String[] args) {
String sql = """
SELECT name, marks FROM students \
WHERE marks > 90""";
System.out.println(sql);
String json = """
{"name": "%s", "marks": %d}""".formatted("Navika", 91);
System.out.println(json);
}
}SELECT name, marks FROM students WHERE marks > 90
{"name": "Navika", "marks": 91}The backslash joins the two SQL lines into one, and formatted fills placeholders, which is handy for templates.
Common Mistakes
Starting the text on the same line as the opening """. That's a compile error; the opening quotes must be followed by a line break.
Being surprised by a trailing newline. If the closing """ is on its own line, the string ends with \n; use print instead of println, or put the quotes at the end of the last line.
Building SQL with formatted and user input. That invites SQL injection; database lessons later use parameters instead.
Practice
- Easy: Write your address as a text block of three lines and print it.
- Harder: Create an HTML snippet as a text block with a
%splaceholder for a name, fill it withformatted("Srihitha"), and print it. Then move the closing"""four spaces left and observe the change.
Key Takeaways
Was this tutorial helpful?