Member-only story
TextBlock: It’s a Java String With Superpowers
Today I discovered Java’s TextBlock. It was love at first sight.
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:
- Simplify the writing of string split in different lines
- Enhance the readability of such strings
- Retro compatibility with existing string objects
- 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.