In this article, I’d like to help you understand the Text Blocks – a standard feature in Java 15 with practical code examples and thorough explanation.

You know, the Java language is very well-known for its verbosity in various areas – one of them is the declaration of multi-line String literals. Let’s consider an example below:

String json = "{\"name\": \"painter\", \"qty\": 18, "
		+ "\"size\": {\"width\": 1, \"height\": 8, \"unit\": \"in\"},"
		+ "\"tags\": [\"writing\", \"pen\"],"
		+ "\"rating\": 7}";
As you can see, this String literal represents a JSON document, embedded in Java code. It is cluttered up by String concatenation operators (+) and escape sequences for double quotes (“) – making the literal difficult to read, and maintenance of this stuffs is very frustrating for developers. Though IDE assists in creating multi-line String literals less painful, it is difficult to read and the cost of maintenance is high.

The good news is, Java 15 cures such developer’s pain by introducing the text block feature that allows programmers to express Strings that span several lines of code while avoiding escape sequences in common cases. The above JSON string literal can be rewritten using a text block as follows:

String json = """
		{
		 "name": "painter", "qty": 18, 
		 "size": {"width": 1, "height": 8, "unit": "in"},
		 "tags": ["writing", "pen"],
		 "rating": 7
		}				
		""";
Brilliant! The String literal looks exactly as a standard JSON document without any concatenation operators and escape sequences, right? Unlike a regular String literal, a textblock begins with 3 double quotes and ends with 3 double quotes.

Moreover, Java compiler will remove the leading and trailing spaces and re-indent the content of a text block. If you print the above JSON string to the console, you will see this output:

{
 "name": "painter", "qty": 18,
 "size": {"width": 1, "height": 8, "unit": "in"},
 "tags": ["writing", "pen"],
 "rating": 7
}
Awesome! So programmers won’t have to write extra code just to remove the unwanted spaces. If you want to keep some trailing spaces, you must use the \s escape sequence. For example:

String languages = """
		Java     \s
		Python   \s
		Ruby     \s
		Javascript
		""";
In this text block, 3 escape sequences \s are used in the first 3 lines, to make each line contains 10 characters including spaces.

In case you don’t want to have new line characters in a text block, you can use the escape character \ (backward slash). For example, in a textblock for a database query:

String query = """
		SELECT p FROM Product p WHERE p.name LIKE %?1% \
		OR p.shortDescription LIKE %?1% \
		OR p.fullDescription LIKE %?1% \
		OR p.brand.name LIKE %?1% \
		OR p.category.name LIKE %?1%
		""";


You can see the \ character is used at the end of each line to tell Java compiler that the String literal continues on the next line (do not interpret new line characters). This is much more convenient than using String concatenation operators (+) for multi-line String literals.

You can also use escape sequences in a text block as in a regular String literal. But with text blocks, I think we don’t have to use escape sequences in most common cases. For example, if you want to use 3 double quotes in a text block, you must escape it like this:

String textBlock = """
		This is an example of a text block in Java:
		String json = \"""
		\t \t {"name": "painter"}
		\t \t\""";
		""";
You see, this text block uses the \””” to represent 3 double quotes and \t for a tab. Print this text block you will see the following output:

This is an example of a text block in Java:
String json = """
	 	 {"name": "painter"}
	 	""";
Okay, so that’s to help you understand text block - a new, standard feature in JDK 15. To summary, below are some key points you should keep in mind:

  • Text block allows the creation of multi-line String literals a lot easier.
  • Text block is not raw string, because the Java compiler will process a text block: normalize line terminators, remove incidental white spaces (both leading and trailing) and interpret escape sequences.
  • A text block is declared inside the opening 3 double quotes and closing 3 double quotes, and the content begins on the next line.
  • Text block introduces 2 new escape sequences: \ for line continuation and \s for not removing trailing spaces.
For the detailed specification of Text block feature, I recommend you to read JEP 378.

To understand how to use text blocks in action, watch the video below:

 

You may be also interested:

 

Other Recommended Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.



Add comment