Skip to content
Java

Text Blocks

Java 15+ multi-line strings.

By EZ4Code Team
text-blockstring

Code

// Text block
String json = """
    {
        "name": "Alice",
        "age": 30,
        "city": "Beijing"
    }
    """;

String sql = """
    SELECT u.name, COUNT(o.id) as order_count
    FROM users u
    LEFT JOIN orders o ON u.id = o.user_id
    WHERE u.active = true
    GROUP BY u.name
    HAVING COUNT(o.id) > 0
    ORDER BY order_count DESC
    """;

// Escaping and formatting
String template = """
    Hello, %s!
    Your balance is $%.2f
    """.formatted("Alice", 1234.5678);

Explanation

Text blocks define multi-line strings with triple quotes, preserving format and supporting escapes.

More Java Snippets