• 🎓 Java Text Blocks Syntax

    With a Touch of French Literature

    Java introduced text blocks to make it easier to write multi-line strings without having to concatenate lines or escape special characters like quotes and newlines.

    🧱 Syntax of a Text Block

    A text block starts with three double-quote characters (""") followed by a line terminator. That means you cannot begin writing content on the same line as the opening triple quotes.

    // ❌ ERROR — single-line text block not allowed
    String quote = """Victor Hugo""";
    
    // ❌ ERROR — line not terminated properly
    String colors = """bleu
                       blanc
                       rouge
                       """;

    ✅ But this is perfectly valid:

    String colors = """
        bleu
        blanc
        rouge
        """;

    This is equivalent to:

    String colors = "bleu\n" +
                    "blanc\n" +
                    "rouge\n";

    📚 Quoting French Authors with Text Blocks

    Let's now look at some classic French quotes using text blocks.

    String hugo = """
        "La liberté commence où l’ignorance finit."
        — Victor Hugo
        """;

    Compared to the traditional way:

    String hugo = "\"La liberté commence où l’ignorance finit.\"\n" +
                  "— Victor Hugo\n";

    Notice how cleaner and more readable the text block version is—no need to escape quotes or manually add newline characters.

    💡 Code Example Inside a Text Block

    Text blocks are also great for embedding code:

    String code = """
        System.out.println("On ne voit bien qu’avec le cœur.");
        // — Antoine de Saint-Exupéry
        """;

    Which would traditionally be written as:

    String code = "System.out.println(\"On ne voit bien qu’avec le cœur.\");\n" +
                  "// — Antoine de Saint-Exupéry\n";

    ✨ Summary

    Text blocks simplify writing multi-line strings, whether you're listing values or quoting literary giants. Just remember:

    🟣 Start with """ followed by a newline.

    🟣 Indentation is preserved but can be trimmed automatically.

    🟣 Quotes inside don’t need to be escaped.