Java Text Blocks

Updated:

This feature can improve your code readability when you migrate from Java 11 LTS to Java 17 LTS. It's easy to implement and has immediate benefits.

Text blocks were initially planned for Java 12 but this feature generated a lot of debates inside the Java Community and it's introduction has been postponed.
After being in preview in JDK 13 and 14 with Java 15 it's the Prime Time for Text Blocks.
It's now an official feature of Java.

Official JEP 378
Oracle's Programmer's Guide to Text Blocks

Why they are introduced? (Official goals)

Simplify the task of writing Java programs by making it easy to express strings that span several lines of source code, while avoiding escape sequences in common cases.

Enhance the readability of strings in Java programs that denote code written in non-Java languages.

No string interpolation!

If you are a JavaScript developer you know how powerful are Template literals, e.g.

 console.log(`For the user : ${user.id}, the sum is ${a + b}!`); 

Text block are not so powerful and they don't support string interpolation.

From the official JEP:

Text blocks do not directly support string interpolation.
Interpolation may be considered in a future JEP.
In the meantime, the new instance method String::formatted aids in situations where interpolation might be desired.

Multiline example

We can express easily multilines texts with a Text Block, a text block is a String included between two """ delimiters.

These snippets work on JShell:

String java14 = "I like this Blog.\nIt show me some examples\nin Java and Angular!"; 
 
String java15 = """ 
        I like this Blog. 
        It show me some examples 
        in Java and Angular!"""; 
 
java14.equals(java15) // => true 

Escape char example

This can be useful if you need to use 'escaped' chars:

String java14 = "I'd like a new post about \"Java 16!\""; 
 
String java15 = """ 
I'd like a new post about "Java 16!" """; 
 
java14.equals(java15); // => true 
 
// Warning there is a 'space' after "Java 16!" in the text block. This space is part of the closing delimiters. 
java15 = """ 
I'd like a new post about "Java 16!""""; // => ERROR: unclosed string literal 
                                         //      ERROR: reached end of file while parsing 
                                         //      Rejected "; 

New methods introduced in Java 15

Thanks to the TextBlocks String gained some new methods in Java 15.

Source Code String

public String stripIndent()

Returns a string whose value is this string, with incidental white space removed from the beginning and end of every line.

public String translateEscapes()

Returns a string whose value is this string, with escape sequences translated as if in a string literal.

public String formatted(Object... args)

Formats using this string as the format string, and the supplied arguments.


WebApp built by Marco using SpringBoot 3.2.4 and Java 21, in a Server in Switzerland