In this post under Java Core, I will introduce you to newly added “Text Block” feature.
This feature was added as part of Java 15.
Pre Java 15, if we have to declare a multiline string we used to declare it as shown below
String result = "Since Java 15, text blocks are \n" + "available as a standard feature.\n" + "With Java 13 and 14, \n" + "we needed to enable it as a preview feature.";
This would result in 7 string objects internally. Further declaring multiline string in this way is always error prone.
We may miss “+” operator or double quote etc.
To ease developer struggles, “Text Block” was introduced.
Below are the points that you should remember when declaring Text Blocks.
1) Text Blocks are nothing but java String objects. So you can call any String class methods on Text Block variables.
2) Text Block starts with 3 double quotes, followed by optional space, and a required new line
3) Text Block ends with 3 double quotes.
4) The ending 3 double quotes can be in the same line as the text or in a newline.
5) If the ending 3 double quotes is not in the same line as text but instead in a new line. A new line is appended to the text inside the text block.
6) If a text contains double or single quotes, you don’t need to escape it.
Below is the complete main method showing the declaration and using of Text Block
Main class
package core.string;public class Example2 { public static void main(String[] args) { String textBlockWithEndingQuotesInNewLine = """ Since Java 15, text blocks are available as a standard feature. With Java 13 and 14, we needed to enable it as a preview feature. """; String textBlockWithEndingQuotesInSameLine = """ Since Java 15, text blocks are available as a standard feature. With Java 13 and 14, we needed to enable it as a preview feature."""; String textBlockWithQuotesAsText = """ He said "Hello". She replied 'Hi'. This is a multiline string with "quotes"."""; System.out.println("-------------------------------------"); System.out.println(textBlockWithEndingQuotesInNewLine); System.out.println("-------------------------------------"); System.out.println(textBlockWithEndingQuotesInSameLine); System.out.println("-------------------------------------"); System.out.println(textBlockWithQuotesAsText); System.out.println("-------------------------------------"); }}
As you can see in the above code, “textBlockWithEndingQuotesInNewLine” variable has text block starting with 3 double quotes, followed by newline. It ends with 3 double quotes in a new line.
“textBlockWithEndingQuotesInSameLine” variable has text block starting with 3 double quotes, followed by newline but it ends with 3 double quotes in the same line as the text.
“textBlockWithQuotesAsText” variable has text block with double quotes as part of the text itself with no escaping.
Below is the output
Output
-------------------------------------Since Java 15, text blocks areavailable as a standard feature.With Java 13 and 14,we needed to enable it as a preview feature.-------------------------------------Since Java 15, text blocks areavailable as a standard feature.With Java 13 and 14,we needed to enable it as a preview feature.-------------------------------------He said "Hello".She replied 'Hi'.This is a multiline string with "quotes".-------------------------------------
As you can see in the output, since the first text block ended with 3 double quotes in a separate line, the output of first text block has a newline appended to it.
In this way we can declare Text Block.