Text Block in Java with Example

Text Block

It’s now possible to create multi-line String literal without the need to concatenate strings on line breaks. This makes text blocks a useful feature for writing long strings that require formatting or readability.


A text block in Java is a new language feature introduced in Java 15 that allows developers to write multi-line strings with improved readability and maintainability.


Text blocks are enclosed between three double quotes (“””) and can contain any number of lines, with leading and trailing whitespaces being removed automatically.

The indentation level of the closing quotes determines the minimum indentation level of the block.


Plain Text String Example:

HTML Code String Example:

SQL Query String Example:

Additional Methods:

Following are the additional methods added to the String class to support text blocks:

  • String::stripIndent(): used to strip away incidental white space from the text block content
  • String::translateEscapes(): used to translate escape sequences
  • String::formatted(Object... args): simplify value substitution in the text block
Share This Post On: