Java

Text Blocks (Java 15+)

admin by @admin ADMIN
Jun 19, 2026
May 31, 2026
Public
0 0 up · 0 down Sign in to vote
Multi-line string literals with proper indentation handling. Stop concatenating newlines by hand. The compiler removes the common leading whitespace automatically.
Java
Raw
class Demo {
    static String html() {
        return """
            <html>
                <body>
                    <h1>Hello</h1>
                </body>
            </html>
            """;
        // The common indentation (12 spaces here) is stripped automatically.
    }

    static String sqlQuery(int userId) {
        return """
            SELECT u.id, u.name, COUNT(o.id) AS order_count
            FROM users u
            LEFT JOIN orders o ON o.user_id = u.id
            WHERE u.id = %d
            GROUP BY u.id, u.name
            """.formatted(userId);
    }

    static String json(String name) {
        return """
            {
              "name": "%s",
              "created_at": "2025-03-12T14:00:00Z"
            }
            """.formatted(name);
    }
}
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.