Text blocks are introduced in java 13 and 14 as a preview feature and with java 15 it is available as standard feature.
What is text block in java ?
Introduction
Java Text Blocks are available from java 15 as a standard feature, which is a string that starts with three double quotes (“””).
Problem statement
Previously, before the release of text blocks, we use multiline string literals like this
package com.slb.java;
public class StringTextBlockDemo {
public static void main(String[] args) {
String str ="This is simplified learning blog, where we provide tutorials for all java releted tech stack" +
"where in we aim at explaing all these features in simplest form possible and" +
"this is just an example of multiline " +
"strings";
System.out.println(str);
}
}
Output
This is simplified learning blog, where we provide tutorials for all java releted tech stackwhere in we aim at explaing all these features in simplest form possible andthis is just an example of multiline strings
Process finished with exit code 0
The issue here is that the code is messy, and It’s difficult to read as well as maintain. Consider a use case wherein you are returning HTML code like this :
package com.slb.java;
public class StringTextBlockDemo {
public static void main(String[] args) {
String str ="This is simplified learning blog, where we provide tutorials for all java releted tech stack" +
"where in we aim at explaing all these features in simplest form possible and" +
"this is just an example of multiline " +
"strings";
System.out.println(str);
String strHtmlContent = "<!DOCTYPE html>\n" +
"<html>\n" +
" <head>\n" +
" <!-- head definitions go here -->\n" +
" </head>\n" +
" <body>\n" +
" <!-- the content goes here -->\n" +
" </body>\n" +
"</html>\n";
}
}
OP
<!DOCTYPE html>
<html>
<head>
<!-- head definitions go here -->
</head>
<body>
<!-- the content goes here -->
</body>
</html>
Now if you look at the first example it was quite okay as compared to this HTML block of code. Another alternate is to use string builder, but that is also not that easy to read and understand.
Java Text Blocks
So to solve this above issue, we have java text blocks to rescue. We will place this HTML code inside three double-quoted string as:
package com.slb.java;
public class StringTextBlockDemo {
public static void main(String[] args) {
String strHtmlContent = """
<!DOCTYPE html>
<html>
<head>
<!-- head definitions go here -->
</head>
<body>
<!-- the content goes here -->
</body>
</html>
""";
System.out.println(strHtmlContent);
}
}
Now, with the above code, it will be easier to read and understand. Note that, java text blocks returns string only.
We can also indent our code.
Escape Sequences in Text Blocks
String blocks have the advantage of escaping common escape sequences like \” and \n as they have no effect on the string and not required.
Backslash at the End of the Line
Java string blocks introduced a new escape sequence to indicate the end of the line using backslash(\).
String strHtmlContent = """
<!DOCTYPE html>
<html>
<head>
<!-- head definitions go here -->
</head>
<body>
<!-- the content goes here -->
</body>
</html>\
""";
Escape Sequence: \s
Another escape sequence you can use to format a text block is “\s”. Trailing spaces are removed from each line by default.
package com.slb.java;
public class StringTextBlockDemo {
public static void main(String[] args) {
String text = """
one a\s
two \s a \s
three a\s""";
System.out.println(text);
}
}
com.slb.java.StringTextBlockDemo
one a
two a
three a
Process finished with exit code 0
Conclusion
Text blocks allow us to use multi line strings in java code, which helps us in improving readability of the code.
Checkout java 17 new features here.