OpenJDK Terminology

Text Blocks

July 06, 2026

Text blocks, finalised in Java 15 (JEP 378), are a multi-line string literal that avoids the need for most escape sequences and manual concatenation when embedding formatted text — SQL, JSON, HTML, XML, code snippets — directly in Java source.

A text block begins with """ followed by a newline, contains the content verbatim (with optional indentation stripping), and closes with """:

String query = """
        SELECT id, name
        FROM users
        WHERE active = true
        ORDER BY name
        """;

The compiler automatically strips the common leading whitespace determined by the position of the closing """, so the indented appearance in source code does not appear in the runtime value. Escape sequences (\n, \t, \\) still work inside text blocks, and two new escape sequences were added: \<newline> to suppress a line break, and \s to preserve trailing whitespace.

Before text blocks, the equivalent code required string concatenation with explicit \n characters, making the SQL or JSON structure hard to read and edit. Text blocks make the intent immediately visible and the content directly copy-pasteable from external tools.

Text blocks are plain String objects at runtime — there is no new type, no runtime overhead, and no change to the String API.

See also: Basic Java Concepts, Records

Comments (0)

Your email address will not be published. Required fields are marked *

Highlight your code snippets using [code lang="language name"] shortcode. Just insert your code between opening and closing tag: [code lang="java"] code [/code]. Or specify another language.

Save my name, email, and website in this browser for the next time I comment.

Comments (0)

Highlight your code snippets using [code lang="language name"] shortcode. Just insert your code between opening and closing tag: [code lang="java"] code [/code]. Or specify another language.

No comments yet. Be the first.

Subscribe to foojay updates:

https://foojay.io/feed/
Copied to the clipboard