5 minutes
Devoxx BE 2025: It Only Starts with a Container & How Abstraction Becomes Reality
At Devoxx Belgium 2025, I was able to talk about what happens after you build your container. In theory, […]
Working with multiline strings in Java has historically been a challenge, often resulting in messy code that is hard to read and maintain. Whether you’re dealing with SQL queries, JSON or HTML templates, managing escape characters and manual line breaks is a cumbersome process. In today’s Nugget Friday, we’ll look at how Text Blocks can redefine how developers handle multiline strings, making code cleaner, more readable and easier to maintain.
Handling multiline strings in Java has traditionally been a pain point for us developers. For example, consider this JSON string:
String json = "{n" +
" "name": "John Doe",n" +
" "age": 30,n" +
" "city": "New York",n" +
" "hobbies": [n" +
" "reading",n" +
" "swimming",n" +
" "hiking"n" +
" ]n" +
"}";
This method is not only error-prone but also hard to read and maintain. It’s also tedious to write and modify, especially for larger strings. Also, it doesn’t preserve the original formatting of the JSON structure.
Introduced as a preview feature in Java 13 and standardized in Java 15, Java Text Blocks provide a clean and intuitive way to work with multiline strings. Let’s rewrite our JSON example using a Text Block:
String json = """
{
"name": "John Doe",
"age": 30,
"city": "New York",
"hobbies": [
"reading",
"swimming",
"hiking"
]
}
""";
Much better, right? The Text Block preserves the formatting, eliminates the need for most escape characters and significantly improves readability. With this, you can now handle multiline strings more elegantly.
Text Blocks are denoted by triple quotes (“””). Here are the key features:
Text Blocks come with some advanced features that provide even more flexibility. These are string interpolation and line control.
You can use the String.format() method or the newer formatted() method for string interpolation:
String name = "Alice";
int age = 30;
String message = """
Hello, %s!
You are %d years old.
""".formatted(name, age);
If you need to include “”” in your Text Block, you can escape it with a backslash:
String code = """
String text = """
Hello, World!
""";
""";
Controlling new lines
You can use at the end of a line to suppress the following new line:
String singleLine = """
This is actually
a single line of text.
""";
While Text Blocks offer a a powerful, neat feature, there are a few things to keep in mind:
Java Text Blocks offer a clean, simple and intuitive solution for working with multiline strings. They improve code readability, reduce errors and make it easier to maintain complex string literals. Whether you’re working with SQL, JSON, HTML or any other multiline text format, Text Blocks can significantly simplify your code.
Next time you find yourself wrestling with concatenated strings (is there anyone who hasn’t been there before?) and escape characters, remember that Text Blocks are here to make your life easier. Give them a try and enjoy the cleaner, more maintainable code they provide!
Happy Nugget Friday and Happy Coding!
Share:
5 minutes
At Devoxx Belgium 2025, I was able to talk about what happens after you build your container. In theory, […]
5 minutes
Welcome aboard the October issue of The Monthly Catch!As the leaves turn and conference season hits full stride, the […]
2 minutes
How outdated Java systems are draining budgets and throttling innovation across financial services? Let’s dig in in this blog […]