Wednesday, March 30, 2022

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

Java 15 – Text blocks, Oracle Java Certification, Oracle Java Career, Oracle Java Skills, Oracle Java Certification, Oracle Java Learning

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…”

Java 15 – Text blocks, Oracle Java Certification, Oracle Java Career, Oracle Java Skills, Oracle Java Certification, Oracle Java Learning
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:

Java 15 – Text blocks, Oracle Java Certification, Oracle Java Career, Oracle Java Skills, Oracle Java Certification, Oracle Java Learning
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:

Java 15 – Text blocks, Oracle Java Certification, Oracle Java Career, Oracle Java Skills, Oracle Java Certification, Oracle Java Learning
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));
    }
);

Source: javacodegeeks.com

Related Posts

0 comments:

Post a Comment