
I have seen a lot of "ugly" code that does artificial string concatenation, so I think the feature definitely addresses an issue.
Holger mentions "the 'text blocks' feature is not very convincing". Ideally, a text block would be indented to match the indentation of the surrounding code.Indeed, finding a good answer in Java 13 is not easy (maybe even impossible) but I think Jorn Vernee addressed the concern gracefully in his reply (new escape sequences in Java 14 provide an acceptable solution). Thus, placing the closing delimiter on the last visible line effectively drops the last \n. This text block is equivalent to is "red\ngreen\nblue". What if you want to represent a multi-line string without that final \n? String name = """ Note that the example above, String name = """ The equivalent string literal would be: String source = "String message = \"Hello, World!\" \n" + Note that there is no need to escape the embedded double quotes. Here's an example of a snippet of Java code within a text block: String source = """ This last example is equivalent to the following string literal: String name = "red\n" + Handling rules (see the section below, Incidental White Space). Requiring the initial line terminator simplifies the indentation The reason for this is that textīlocks are primarily designed to support multi-line strings, and The contents of the text block follow the three opening double-quotes You can't put a text block on a single line, nor can Of shoes - and ships - and sealing-wax -Ī text block begins with three double-quote characters followed by a Using text blocks removes much of the clutter: // BETTER "Of shoes - and ships - and sealing-wax -\n" + String message = "'The time has come,' the Walrus said,\n" + In this case there isĬonsiderable clutter from quotation marks, newline escapes, and Literal is used to represent a multi-line string. Smith""".substring(8).equals("Smith") // trueĪ text block can be used in place of a string literal to improve the
String methods may be applied to a text block: """ Text blocks may be used as a method argument: (""" String together = str + " and " + tb + "." ForĮxample, text blocks may be intermixed with string literals in a stringĬoncatentation expression: String str = "The old" Text blocks can be used anywhere a string literal can be used. Both dqName and tbName intern to the same string Continuing with dqName and tbName from the examples above, // Both dqName and tbName are strings of equal value This includes object representation and interning. With the same characteristics as a traditional double quoted string. The object produced from a text block is a That can be used anywhere a traditional double quoted string literal canīe used. Obstructions, allowing you to embed code snippets and text sequencesĪ text block is an alternative form of Java string representation Required a tangled mess of explicit line terminators, stringĬoncatenations, and delimiters. In earlier releases of the JDK, embedding multi-line code snippets
This guide assembles practical usage advice for text blocks, along withĪ text block's principalis munus is to provide clarity by way of minimizing the Java syntax required to render a string that spans multiple lines. While the JEP explains the feature in greatĭetail, it's not always apparent how the feature can and should be used. JEP 378 adds the language feature text blocks Detecting Potential Issues with White Space.