In Java 13 they want to add “text blocks”

Original author: Jim Laskey
  • Transfer
Blocks of text are planned for release in Java 13. This was announced in JEP 355 .

Blocks of text is 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 studies starting in JEP 326 (unprocessed string literals were recalled).

Jep 355 goals

  • Simplify writing Java code by avoiding escape sequences.
  • Improving code readability.

What the JEP 355 is definitely not 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 "+".
  • Blocks of text do not support line interpolation. This feature may be added in subsequent JEPs.


In Java for embedding XML, HTML, JSON, etc. significant editing with shielding and string concatenation is usually required. A 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 line consisting of several "lines" and without special mess. characters. In fact, this is a two-dimensional block of text, not a one-dimensional sequence of characters.

Syntax and Description

Blocks of lines are framed """both """on the right and on the left. The content of the block begins with the first character after """and ends with the last character before """. Triple quotes are selected to make it clear that these are lines of text, but to distinguish them from a regular string literal ( "...").

Blocks can contain quotation marks ( ") directly, without a slash ( \). You can use and \", but this is not recommended.

Line wrapping is done automatically. The use of \ n is permitted but not recommended.

line 1
line 2
line 3


"line 1\nline 2\nline 3\n"


"line 1\n" +
"line 2\n" +
"line 3\n"

Here is an example of an empty block of text:

String empty = """

Here is the bad practice of using blocks of text:

String a = """""";   
String b = """ """;
String c = """
String d = """        
           abc \ def

Escape sequences in blocks of text

Escape sequences are interpreted. This means that developers can write escape sequences, for example, \ninside blocks.



String html = """

Hello, world


Old way
String html = "\n" +
              "    \n" +

Hello, world

\n" + " \n" + "\n";


String query = """
               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";


ScriptEngine engine = new ScriptEngineManager().getEngineByName("js");
Object obj = engine.eval("""
                         function hello() {
                             print('"Hello, world"');

Old way
ScriptEngine engine = new ScriptEngineManager().getEngineByName("js");
Object obj = engine.eval("function hello() {\n" +
                         "    print('\"Hello, world\"');\n" +
                         "}\n" +
                         "\n" +

Only registered users can participate in the survey. Please come in.

Do you like this feature?

  • 75.3% Yes 290
  • 4.1% No 16
  • 8.3% I don't care 32
  • 2% Yes, but I will not use it 8
  • 10.1% I do not write in Java 39

Also popular now: