Java: Text Block (JEP 378)

Easier & Readable Strings

Java:  Text Block (JEP 378)

Subscribe to my newsletter and never miss my upcoming articles

Text block is a new feature added from Java 15. As per JEP 378

A text block is a multi-line string literal that avoids the need for most escape sequences, automatically formats the string in a predictable way, and gives the developer control over the format when desired.

Source https://openjdk.java.net/jeps/378

Before Java 15, it has not supported multiline text. It has been difficult and ugly to write XML, JSON, SQL as string literals, where multiline make more readable code. With Java 15 +, we can use triple " to define a multiline string. It makes code more readable and cleaner. Let us compare multiline Strings before & after Java 15. Look at the below example of an HTML document

Without text block

String htmlWithoutTextBlock="<!DOCTYPE html>\n" +
            "<html>\n" +
            "   <body>\n" +
            "      <p>This is java example of HTML text block with and without Text block.</p>\n" +
            "   </body>\n" +
            "</html>";

With text block

String htmlWithTextBlock= """
            <!DOCTYPE html>
            <html>
               <body>
                  <p>This is java example of HTML text block with and without Text block.</p>
               </body>
            </html>""";

The newer one with text block is more readable and easy to understand.

Formatting the Text Block

Text block supports String formatting as the String without text block. Let us look at another example with JSON.

   System.out.println("Formatting Text Block ");
        String superHero = """
                {
                    "name":"%s",
                    "prefix":"%s",
                    "suffix":"%s",
                    "power":"%s"
                }
                """;
        System.out.println(String.format(superHero, "Invisible Force",
                "Captain", "Lightening", "Invisible"));

The output of the above code will be

Formatting Text Block 
{
    "name":"Invisible Force",
    "prefix":"Captain",
    "suffix":"Lightening",
    "power":"Invisible"
}

Long lines

There are chances you have a long line to be added in java file. Rather than putting in one line, you can use \ to break it in code but to be taken as a single long line.

System.out.println("Very Long String as a single line");
        String veryLongString= """
                Once up a time, there used to be java strings without text blocks,\
                they were difficult to read and comprehend, \
                  then came java 15 and it changed the game""";
        System.out.println(veryLongString);

The output of the above code will be as following, Do note that it's a single line and not three different lines. If you don't add \ at the list it will print as it's written in different lines.

Very Long String as a single line
Once up a time, there used to be java strings without text blocks,they were difficult to read and comprehend,   then came java 15 and it changed the game
 
Share this