BackendBench
Coffee

Unravel โ€บ Java concepts โ€บ Language basics

Java language

Text blocks

A text block is a multi-line string literal between triple quotes (""").

๐Ÿ”Ž Unravel this example

At a glance

Mental model

Pasting a paragraph from a document as it is, instead of typing it as one long line with \n and + between the pieces.

What happens, in order
  1. 1The opening """ must be followed by a line breakThe text starts on the next line and ends at the closing """.
  2. 2The compiler strips the indentation that all lines share (measured against the closing delimiter too), so you can indent the block to match your code
  3. 3Escapes still work (\n, \t), and \ at the end of a line joins it with the nextUse .formatted(args) on the block to fill in values: %d for a number, %s for text.
Remember
Watch out
Putting text on the same line as the opening """Expecting indentation of the closing delimiter to be ignored; it controls how much is strippedConcatenating user input into SQL or JSON inside the block
Example (highlighted lines explain it)
public class Queries {  static String orders(int limit) {    return """        SELECT id, total        FROM orders        WHERE status = 'OPEN'        LIMIT %d        """.formatted(limit);  }}
Read the full explanation (about 1 min)

A text block is a multi-line string literal between triple quotes ("""). It keeps line breaks and removes the common indentation, which makes embedded JSON, SQL and HTML readable.

A mental model

Pasting a paragraph from a document as it is, instead of typing it as one long line with \n and + between the pieces.

How it works

The opening """ must be followed by a line break. The text starts on the next line and ends at the closing """.

The compiler strips the indentation that all lines share (measured against the closing delimiter too), so you can indent the block to match your code.

Escapes still work (\n, \t), and \ at the end of a line joins it with the next. Use .formatted(args) on the block to fill in values: %d for a number, %s for text.

Going deeper

Never build SQL by gluing user input into a text block; use placeholders with a PreparedStatement. A text block makes the SQL readable but does not make it safe.

Trailing spaces on each line are removed. Line endings are always normalised to \n, regardless of the operating system.

Under the hood

A text block is an ordinary String constant. All the indentation processing and escape handling happens at compile time, so there is no runtime cost.

Common mistakes

  • Putting text on the same line as the opening """.
  • Expecting indentation of the closing delimiter to be ignored; it controls how much is stripped.
  • Concatenating user input into SQL or JSON inside the block.

How Unravel recognises it

When you paste code, Unravel looks for these signs of text blocks. Spotting them yourself is a good habit when you read unfamiliar code:

Part of a learning path

Related concepts

Frequently asked questions

What is Text blocks?

A text block is a multi-line string literal between triple quotes ("""). It keeps line breaks and removes the common indentation, which makes embedded JSON, SQL and HTML readable.

What are the most common mistakes with Text blocks?

Putting text on the same line as the opening """. Expecting indentation of the closing delimiter to be ignored; it controls how much is stripped. Concatenating user input into SQL or JSON inside the block.

How can I see Text blocks in real code?

Paste your own Java into Unravel and it will point out where text blocks appears, explain how it works at the detail level you choose, and draw the flow of the code around it.