DEV Community

Cover image for Simplifying Multi-line Strings with Text Blocks in Java
Igor Fraga
Igor Fraga

Posted on • Originally published at igorfragadev.com

1

Simplifying Multi-line Strings with Text Blocks in Java

Hello, Java developers! I'm Igor Fraga, and I'm back after a short pause of some days that I've been busy with the book I'm writing (I will bring it here very soon)

This is the third post from a series called “The upgraded Java Developer” about the latest Java programming language enhancements to be up to date and use these features in your daily work. We will look into some enhancements Java had during these last years regarding String handling before jumping into more advanced stuff.

Today we're exploring Text Blocks, a feature that has revolutionized how we handle multi-line strings in Java. Introduced as a preview feature in Java 13 and standardized in Java 15, Text Blocks are now a robust part of Java 21, offering a cleaner and more intuitive way to work with complex string literals.

What are Text Blocks?

Text Blocks are a new way to represent multi-line string literals in Java. They allow you to write strings spanning multiple lines without the need for most escape sequences or explicit line breaks.

How Text Blocks Work

Here's an example of how to use a Text Block:

    String json = """
        {
            "name": "John Doe",
            "age": 30,
            "city": "New York",
            "hobbies": [
                "reading",
                "swimming",
                "hiking"
            ]
        }
        """;
Enter fullscreen mode Exit fullscreen mode

In this example, the Text Block is denoted by triple quotes (""") and preserves the formatting of the JSON structure.

Why are Text Blocks Better?

Compared to traditional string concatenation, Text Blocks offer several advantages:

  1. Improved Readability: They eliminate the need for escape characters and string concatenation operators.

  2. Preserved Formatting: The structure of the text is visually represented in the code.

  3. Reduced Errors: Fewer escape sequences mean fewer opportunities for syntax errors.

  4. Better Maintainability: Updating multi-line strings becomes much easier.

Best Practices

  1. Use Text Blocks for multi-line strings like SQL queries, JSON, HTML, or any formatted text.

  2. Be mindful of indentation, as it affects the resulting string.

  3. Use the \ character at the end of a line to suppress the following new line if needed.

Conclusion

Text Blocks, fully supported in Java 21, represent a significant improvement in how we handle multi-line strings in Java. They make our code cleaner, more readable, and easier to maintain, especially when dealing with structured text formats like JSON, SQL, or HTML.

In our next article, we'll explore String Templates, that is the evolution of Text Blocks that has been introduced in Java 21 and how it has evolved from String concatenation, to Text Blocks and finally into String Templates. See you then!

AWS Q Developer image

Your AI Code Assistant

Implement features, document your code, or refactor your projects.
Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

Top comments (1)

Collapse
 
xzel profile image
Axel Howind

Thanks for spreading the word about text blocks, a very useful feature.

A note about your next article: The preview feature String Templates has been removed in Java 23. It’s not available in either Java 23 and Java 24, so it might be better to change the subject of your next article as programs using that feature will not run on the latest java versions on probably also not on the next LTS to be released later this year. For details: bugs.openjdk.org/browse/JDK-8329949

It might be added back in a changed form later, but I think developers shouldn’t invest much time learning about it just yet as their code will definitely break once they update their java version beyond 22.

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

If you found this post useful, consider leaving a ❤️ or a nice comment!

Got it