Skip to content
Advertisement

What is the Text Blocks (or Multiline Strings) feature in Java?

Java SE 13 introduced Text Blocks (or Multiline Strings) feature. What are its differences and the similarities with the existing string representation?

Advertisement

Answer

What is a text block?

A text block is a multi-line string literal and the feature offers a clean way to format the string in a predictable way, without using most of the escape sequences. It starts and ends with a """ (three double-quotes marks) e.g.

JavaScript

Output:

JavaScript

With the traditional string representation, the code would look like

JavaScript

Another key difference is that a text block begins with three double-quote characters followed by a line terminator which is not the case with the traditional string representation. It means

  1. The text block can not be put on a single line.

  2. The content of the text block can not follow the three opening double-quotes on the same line.

    JavaScript

This was a much-needed feature in Java.

A note about how it compares with text block in Kotlin (a modern JVM-based language)?

  • Kotlin does not have any of the two above mentioned constraints.
  • Kotlin does not require escaping a inside the text block which makes writing a RegEx expression easier e.g. the following is a valid text block in Kotlin but invalid in Java:
JavaScript

In Java, one will have to write it as

JavaScript

A note about indentation:

The compiler shifts the complete text block to the left and then retains the specified spacing.

JavaScript

Demo:

JavaScript

Output:

JavaScript

Is it available only as a Preview Feature?

It remained available in Java SE 13 and Java SE 14 as a Preview Feature and has been standardised with Java SE 15. With Java SE 13 and 14, like any Preview Feature, it has to be compiled and executed with --enable-preview option e.g.

To compile:

JavaScript

To execute:

JavaScript

Are they stored in string pool?

Yes, they are. The text blocks are compiled to the same type as that of the traditional String value i.e. the byte code does not distinguish between a traditional String value and text block.

JavaScript

Output:

JavaScript

Can a text block be concatenated with another string?

Yes, a text block can be concatenated to a traditional string value or another text block in the same way, the traditional String values are concatenated. As already described above, the byte code does not distinguish between a traditional String value and text block.

JavaScript

Output:

JavaScript

Note that I have put whitespace after Hello in str1 and another whitespace before Java rocks! in str3.

Does it support Escape Sequences?

Yes, the escape sequences will be evaluated in the traditional way e.g.

JavaScript

Output:

JavaScript

Does it support replaceable parameter?

Yes, you can replace a parameter in the text block using %s or $<<replaceable-parameter>> as shown below:

JavaScript

Output:

JavaScript
Advertisement