JEP-355 was an excellent change brought in Java 13 as a preview to simplify the HTML code piece in Java. But, now, Java 14 has come up with a second preview in Text Blocks with two more newly added escape sequences. This change is present under JEP-368. You can download Java 14 by clicking here.

  • \<end-of-line> for newlines (line terminator)
  • \s for single space (white space)


Java 14 - Text Blocks - Key Change for HTML...!!! Share on X

Check out: JAVA 13 Features with examples

JEP 368 – Text Blocks

In Java 13, JEP 355 allows you to create a multiline string or you can say a text-block. You need to write a multiline string inside a pair of triple double-quotes. But, now, in Java 14, JEP 368 has new line & white space sequences too. Let’s check it.

Let’s see the below code that differentiates between code before Java13, In Java13 and finally IN JAVA 14

public class TextBlock {

    public static void main(String[] args) {

        String beforeJava13 = "<html>\n" +

                      "   <body>\n" +

                      "     <div>\n" +

                      "      <p>Howdy, Techndeckers</p>\n" +

                      "      <p>How r u?</p>\n" +

                      "      <p>I am using version before Java 13</p>\n" +

                      "     </div>\n" +

                      "   </body>\n" +

                      "</html>\n";



        String InJava13 = """

                        <html>

                            <body>

                              <div>

                                <p>Howdy, Techndeckers</p>

                                <p>How r u?</p>

                                <p>I am using version 13</p>

                              </div>

                            </body>

                        </html>

                        """;



        String InJava14 = """

                        <html>

                            <body>\

                              <div>\

                                <p>Howdy, \s Techndeck</p>\

                                <p>How r u?, \s </p>\

                                <p>I am using \s\s\s\s\s\s version 14</p>\

                              </div>

                            </body>

                        </html>

                        """;

        System.out.println(beforeJava13);

        System.out.println(InJava13);

        System.out.println(InJava14);

    }

}

Output:

<html>

   <body>

     <div>

      <p>Howdy, Techndeckers</p>

      <p>How r u?</p>

      <p>I am using version before Java 13</p>

     </div>

   </body>

</html>




<html>

    <body>

      <div>

        <p>Howdy, Techndeckers</p>

        <p>How r u?</p>

        <p>I am using version 13</p>

      </div>

    </body>

</html>




<html>

    <body>      <div>        <p>Howdy,   Techndeck</p>                                <p>How r u?,   </p>                                <p>I am using        version 14</p>                              </div>

    </body>

</html>

Java 14 - Text Blocks - Key Change for HTML...!!! Share on X

Other Useful References:

    • What is new in Java 13

Author

  • Deepak Verma

    Deepak Verma is a Test Automation Consultant and Software development Engineer for more than 10 years. His mission is to help you become an In-demand full stack automation tester.

    He is also the founder of Techndeck, a blog and online coaching platform dedicated to helping you succeed with all the automation basics to advanced testing automation tricks.

    View all posts