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...!!! Click To Tweet
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | <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...!!! Click To Tweet
Other Useful References:
- What is new in Java 13