Modern Java Evolution

Text Blocks

Text blocks (Java 15) hold multi-line strings between """ delimiters: no escape ladders, automatic incidental-indentation stripping, and readable embedded JSON, SQL, and HTML.
  • Open with """ + newline; close alignment controls indentation stripping
  • The closing delimiter's column sets the left margin — move it to control indentation
  • \ at line end joins lines (no newline); \s keeps trailing spaces
  • Quotes inside need no escaping; \n, \t still work if wanted
  • It's still a String — same type, same methods, interning included
Before and after
String jsonOld = "{\n" +
        "  \"name\": \"Duke\",\n" +
        "  \"role\": \"mascot\"\n" +
        "}";

String json = """
        {
          "name": "Duke",
          "role": "mascot"
        }
        """;   // closing delimiter position strips the common indent

The indentation algorithm: the compiler finds the minimal indentation across all lines including the closing delimiter line, and strips it — so out-denting the closing """ preserves visual nesting in source while producing flush-left output. String.stripIndent(), translateEscapes(), and formatted() are the companion methods; """…%s…""".formatted(value) covers interpolation until/unless a dedicated feature lands.

Sources
  • Core Java, Volume I: Fundamentals (13th ed.)Ch. 3.6 — Strings (text blocks)
  • Learning Java (6th ed.)Ch. 8 — Text and Core Utilities