A Text Block is a multi-line string literal that avoids the need for most escape sequences.
In another word, its makes multi-line string literals more readable.
Text Block is created with the goal of – Simplifying the task of writing a java program by making it easy to express strings that span several lines of source code – Enhance the readability of strings that denotes code written in non-java languages such as HTML, SQL, Javascript, etc.
It was released as preview in java 13 & 14 but from java 15 it is standard feature.
Use Cases
The best way to explain would be to consider how we embed a snippet of HTML, XML, SQL, or JSON in a string literal.
For example,
Text Block
"""
line 1
line 2
line 3
"""This is equivalent to "line 1\n" +
"line 2\n" +
"line 3\n"
We often see SQL strings like below in the DAO layer, which when executed on the database either through the JPA provider or directly with PreparedStatement bypassing JPA.
String query = "SELECT `EMP_ID`, `LAST_NAME` FROM `EMPLOYEE_TB`\n" +
"WHERE `CITY` = 'INDIANAPOLIS'\n" +
"ORDER BY `EMP_ID`, `LAST_NAME`;\n";
Now with Text Block, we can write the above query in a more readable format and don’t need to provide a newline character and don’t need to provide an escape character.
We can use three double-quote characters (""") followed by zero or more whitespaces followed by a line terminator. We can close the string with three double-quote characters.
String query = """
SELECT `EMP_ID`, `LAST_NAME` FROM `EMPLOYEE_TB`
WHERE `CITY` = 'INDIANAPOLIS'
ORDER BY `EMP_ID`, `LAST_NAME`;
""";
The above example might not look a great deal to some, since the example is very simple but if think when we have a very complex String for example below JSON string, and if we write this JSON as String literal it would not be much readable.
If we represent it as String in Java without Text Block, then it would not be so developer-friendly.
But if we use java Text Block then, it looks more developer-friendly and much more readable.
Conclusion
With Text Block java readability increased further. Since Java 8 , Java designers have worked alot towards making java less versbose and more readable , Text Block is also one of such effort.
This is not the game changer feature but its java designers commitment to make this #1 language more readable and reduce verbosity.
Bonus Tip
If you want to upskill your Java, you should definitely check out this bestseller course
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
Cookie
Duration
Description
cookielawinfo-checkbox-analytics
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional
11 months
The cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy
11 months
The cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.