{{announcement.body}}
{{announcement.title}}

JEP 355 Text Blocks in JDK 13

DZone 's Guide to

JEP 355 Text Blocks in JDK 13

Check out one of the more exciting features from JDK 13 — text blocks!

· Java Zone ·
Free Resource

JAVA 13

Check out one of the more exciting features from JDK 13 — text blocks!

JDK 13 went GA on September 17th, 2019 and the prominent new features are listed here. One of the new features is "text blocks." This allows writing multiline strings easily without the need for concatenation while splitting into different lines.

You may also like: [DZone Refcard] Java 13

Lets quickly look at the different ways of creating multiline strings:

String aBlock = """
SELECT id, first_name, last_name, dob
FROM person
WHERE id = ?
""";

String aIndentedBlock = """
    SELECT id, first_name, last_name, dob
    FROM person
    WHERE id = ?
""";

String anotherBlock = """
SELECT id, first_name, last_name, dob
FROM person
WHERE id = ?""";

System.out.print(aBlock);
System.out.print(aIndentedBlock);
System.out.print(anotherBlock);
System.out.println("This comes in the same line");


These are some of the invalid ways to write multiline string:

//        String thisIsInvalid = """This is invalid""";
//        String thisIsALsoInvalid = """THis is also invalid
//        """;


The text cannot start immediately after the opening quotes of the block i.e `"""` instead it has to start in the next line.

We can include " , ' in the multiline string:

System.out.println("""
        Block containing "" ' '
        """);


The end of block quotes can end in the same line as the text of in a new line:

String thisIsValid = """
This is valid""";


The complete code for this can be found here.

Further Reading

[DZone Refcard] Java 13

81 New Features and APIs in JDK 13

Java Text Blocks

Chatting With Oracle About the New Java 13 Release

Topics:
java ,java 13 ,jep ,text blocks ,news ,jep 355

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}