In Java 13, new way of defining multiline, large, pre-formatted texts is introduced which is called ‘Text Blocks’. This has been introduced as part of JEP 355. Lets look at examples of this text blocks.
Setup for preview feature
Text blocks in JDK 13 are preview only features i.e. this feature is not enabled by default. If you directly write code to use this, it will give compiler error Text Blocks is a preview feature and disabled by default. Use --enable-preview to enable
If you are using eclipse IDE, then right click on project > Properties > Java Compiler > Uncheck “Use default compliance settings” > Check “Enable preview feature for Java 13”
After enabling preview feature, compiler will continue to give warning
You are using a preview language feature that may or may not be supported in a future release wherever preview features are used.
Multiline HTML with indentation Example
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 |
/* * Classic way of multiline String. * * Need to use \n or \r. Need to use '+' if we want to separate out each line * for better readability. * * Formatting & alignment might get modified due to code formatter. */ String html_classic = "<html>\r\n" + " <head>\r\n" + " <title>Its All Binary</title>\r\n" + " </head>\r\n" + " <body>\r\n" + " <h1>Its All Binary</h1>\r\n" + " Welcome everyone\r\n" + " </body>\r\n" + "</html>"; System.out.println("\nhtml_classic:\n" + html_classic); /* * Java 13 way using text block with """ * * No need of \n or \r. Respective indentation is maintained. * * Formatting & alignment will not be modified by code formatter. * */ String html_java13 = """ <html> <head> <title>Its All Binary</title> </head> <body> <h1>Its All Binary</h1> Welcome everyone </body> </html> """; System.out.println("\nhtml_java13:\n" + html_java13); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
html_classic: <html> <head> <title>Its All Binary</title> </head> <body> <h1>Its All Binary</h1> Welcome everyone </body> </html> html_java13: <html> <head> <title>Its All Binary</title> </head> <body> <h1>Its All Binary</h1> Welcome everyone </body> </html> |
Multiline indented SQL Query Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
/* * Classic way of multiline String for SQLs. */ String sql_classic = "SELECT first_name, \r\n" + " last_name \r\n" + "FROM employee \r\n" + "WHERE department = \"IT\" \r\n" + " AND designation = \"developer\" "; System.out.println("\nsql_classic:\n" + sql_classic); /* * Java 13 way using text block with """ for SQLs. */ String sql_java13 = """ SELECT first_name, last_name FROM employee WHERE department = "IT" AND designation = "developer" """; System.out.println("\nsql_java13:\n" + sql_java13); |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
sql_classic: SELECT first_name, last_name FROM employee WHERE department = "IT" AND designation = "developer" sql_java13: SELECT first_name, last_name FROM employee WHERE department = "IT" AND designation = "developer" |
Paragraphs with + for longer text lines.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
/* * Classic way of multiline String for paragraphs with +. */ String para_classic = "Java is a programming language. Java is a Object Oriented Programming language (OOPs). " + "Java is platform independent. Latest java version is Java version 13. " + "Text block is a new feature added in Java 13."; System.out.println("\npara_classic:\n" + para_classic); /* * Java 13 way using text block with """ for paragraphs with +. */ String para_java13 = """ Java is a programming language. Java is a Object Oriented Programming language (OOPs).""" + """ Java is platform independent. Latest java version is Java version 13. Text block is a new feature added in Java 13."""; System.out.println("\npara_java13:\n" + para_java13); |
1 2 3 4 5 |
para_classic: Java is a programming language. Java is a Object Oriented Programming language (OOPs). Java is platform independent. Latest java version is Java version 13. Text block is a new feature added in Java 13. para_java13: Java is a programming language. Java is a Object Oriented Programming language (OOPs).Java is platform independent. Latest java version is Java version 13. Text block is a new feature added in Java 13. |
Mix classic string way & Java 13 feature
1 2 3 4 5 6 7 |
/* * Mix classic way & new Java 13 way of string & text block */ String para_mix = """ Java is a programming language. Java is a Object Oriented Programming language (OOPs).""" + "Java is platform independent. Latest java version is Java version 13. Text block is a new feature added in Java 13."; System.out.println("\npara_mix:\n" + para_mix); |
1 2 |
para_mix: Java is a programming language. Java is a Object Oriented Programming language (OOPs).Java is platform independent. Latest java version is Java version 13. Text block is a new feature added in Java 13. |