Member-only story

TextBlock: It’s a Java String With Superpowers

Today I discovered Java’s TextBlock. It was love at first sight.

Gaetano Piazzolla
2 min readFeb 26, 2022
Superman ASCII Art :)

Apparently, It’s an old feature of Java previously called RowString, that wasn’t ready to be shipped because during the testing phase showed several problems.

It was finally delivered in JEP 378 and included in the JDK 15 in September 2020 as a FINAL and PERMANENT feature.

Goals:

  1. Simplify the writing of string split in different lines
  2. Enhance the readability of such strings
  3. Retro compatibility with existing string objects
  4. Intelligent control of spaces and newlines with special escape characters

+Bonus: You don’t need to escape the character “ (!!!this is awesome!!!) ++Bonus: The compiler will automatically indent the TextBlock

Let’s see TextBlocks in action.

Prerequisites: we need to install the JDK 15 (or more recent versions) and be sure that we are using the correct one by running:

$ java --version

If you are using an IDE like Intellij (it shouldn’t be considered as a good choice by default) remember also to configure correctly the Project SDK and the Project Language Level.

--

--

Gaetano Piazzolla
Gaetano Piazzolla

Written by Gaetano Piazzolla

Tech. Writer and Passionate Developer.

No responses yet