Home » Java » Core Java » Java 15 – Text blocks

About Mohamed Sanaulla

Java 15 – Text blocks

Text blocks are all about writing multi-line strings in a clean and readable way. This was added as part of JEP 378 in Java 15. One can read the detailed information of the feature from the JEP details.

In this post, I will show you how to use text blocks and some things to keep in mind while using them.

Firstly, the boundary of a text block is defined by using """ and """. Anything between this is your string literal as shown below

String userDetailSQL = """
    SELECT u.username, u.first_name, u.last_name, r.privilege
    FROM app_user u
    LEFT OUTER JOIN app_user_privilege up ON up.username = r.username
    WHERE u.username = ?
    """;
System.out.print(userDetailSQL);

The above would simply print the query defined in userDetailSQL as shown below

Some important things to note here

Point 1

A new line is added at the end of the string only if the closing """ is in the new line. So if we had the below:

String userDetailSQL = """
    SELECT u.username, u.first_name, u.last_name, r.privilege
    FROM app_user u
    LEFT OUTER JOIN app_user_privilege up ON up.username = r.username
    WHERE u.username = ?""";
System.out.print(userDetailSQL);

The output would have been (see the absence of an extra line above “Process finished…”

String printed without the new line appended to the string

Point 2

Suppose we were to write the query with indentation as shown below:

String userDetailSQLWithWhitespace = """
    SELECT
        u.username,
        u.first_name,
        u.last_name,
        r.privilege
    FROM app_user u
    LEFT OUTER JOIN app_user_privilege up 
        ON up.username = r.username
    WHERE u.username = ? 
    """;
System.out.println(userDetailSQLWithWhitespace);

The compiler will preserve any indentation done away from the line drawn upwards vertically starting from the closing """. So the output in the above case can be:

The string printed by preserving the indents

Point 3

We can force a string to be indented by using the indent method as shown below:

String userDetailSQL = """
        SELECT u.username, u.first_name, u.last_name, r.privilege
        FROM app_user u
        LEFT OUTER JOIN app_user_privilege up ON up.username = r.username
        WHERE u.username = ?
        """;
System.out.println(userDetailSQL.indent(3));

And the output of this would be:

Spaces have been added to adjust the text based on the indent

Point 4

You can create an empty text block, though its not going to be useful. You would do it as:

//Correct empty text block
String emptyTextBlock = """
    """;

And not as

//Wrong empty text block
String emptyTextBlockWrong = """""";

Point 5

You can concatenate text blocks with normal strings and also create formatted text blocks as shown below:

IntStream.range(1, 100).forEach(
    i -> {
        String message = """
                INSERT INTO app_user(username, first_name, last_name, password)\s
                VALUES('user%d', 'First %d', 'Last %d', '12345-%d');
                """;
        System.out.println(String.format(message, i, i, i,i));
    }
);
Published on Java Code Geeks with permission by Mohamed Sanaulla, partner at our JCG program. See the original article here: Java 15 – Text blocks

Opinions expressed by Java Code Geeks contributors are their own.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions

 

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments