Text blocks
Definition​
A text block is a multi-line string written between triple quotes. It keeps line breaks and removes the common indentation, so JSON, SQL and HTML are easy to read in code without \n and escaped quotes.
Example code​
A JSON string without escaping
class Demo {
public static void main(String[] args) {
String name = "Asha";
String json = """
{
"name": "%s",
"active": true
}
""".formatted(name);
String sql = """
SELECT id, name
FROM users
WHERE active = true
""";
System.out.print(json);
System.out.print(sql);
}
}
// Output:
// {
// "name": "Asha",
// "active": true
// }
// SELECT id, name
// FROM users
// WHERE active = true
Finished this page? Track your progress (saved only in your browser).