Blocks of text are scheduled to appear in Java 13. This has been learned from
JEP 355 .
Text blocks are a multi-line string literal that eliminates the need for escaping most special characters and automatically makes line breaks.
This is a further attempt at research that began in JEP 326 (raw string literals was recalled).
')

JEP 355 targets
- Simplify writing Java code avoiding escape sequences.
- Enhance code readability.
What the JEP 355 is not exactly trying to achieve
- This is not an attempt to define a new reference type, as
java.lang.String
. - This is not an attempt to override the string operator "+".
- Text blocks do not support string interpolation. This feature can be added in subsequent JEPs.
Motivation
In Java, for embedding XML, HTML, JSON, etc. objects. usually requires significant editing with escaping and string concatenation. The fragment is often difficult to read and difficult to maintain.
Accordingly, the new JEP improves both readability and adds the ability to write a wide class of Java programs - using a string consisting of several "lines" and without a special visual disorder. characters. In essence, this is a two-dimensional block of text, not a one-dimensional sequence of characters.
Syntax and Description
Blocks of lines are framed
"""
and
"""
right and left. The block content begins with the first character after
"""
and ends at the last character before
"""
. Triple quotes are selected to make it clear that these are strings of text, but in order to be able to distinguish them from the usual string literal (
"..."
).
Blocks can contain quotation marks (
"
) directly, without a slash (
\
). You can also use
\"
, but this is not recommended.
Line wrapping is done automatically. Using \ n is allowed but not recommended.
""" line 1 line 2 line 3 """
similarly
"line 1\nline 2\nline 3\n"
or
"line 1\n" + "line 2\n" + "line 3\n"
Here is an example of an empty block of text:
String empty = """ """;
Here is a bad practice of using blocks of text:
String a = """"""; String b = """ """; String c = """ "; String d = """ abc \ def """;
Escape sequences in text blocks
Escape sequences are interpreted. This means that developers can write escape sequences, for example,
\n
inside blocks.
Examples
HTML
String html = """ <html> <body> <p>Hello, world</p> </body> </html> """;
Old way String html = "<html>\n" + " <body>\n" + " <p>Hello, world</p>\n" + " </body>\n" + "</html>\n";
SQL
String query = """ SELECT `EMP_ID`, `LAST_NAME` FROM `EMPLOYEE_TB` WHERE `CITY` = 'INDIANAPOLIS' ORDER BY `EMP_ID`, `LAST_NAME`; """;
Old way String query = "SELECT `EMP_ID`, `LAST_NAME` FROM `EMPLOYEE_TB`\n" + "WHERE `CITY` = 'INDIANAPOLIS'\n" + "ORDER BY `EMP_ID`, `LAST_NAME`;\n";
Script
ScriptEngine engine = new ScriptEngineManager().getEngineByName("js"); Object obj = engine.eval(""" function hello() { print('"Hello, world"'); } hello(); """);
Old way ScriptEngine engine = new ScriptEngineManager().getEngineByName("js"); Object obj = engine.eval("function hello() {\n" + " print('\"Hello, world\"');\n" + "}\n" + "\n" + "hello();\n");