

Using jshell to experiment with text blocks requires using the -enable-preview flag as well: jshell -enable-preview In order to use text blocks in Java code, you must use the -enable-preview and -source 13 flags on the javac command line and the -enable-preview flag on the java command line: javac -enable-preview -source 13 -d classes TextBlockExample.java

Text blocks exist as a Preview feature of the Java Language. 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 considerable clutter from quotation marks, newline escapes, and concatentation operators: // ORIGINAL This primarily occurs when a string 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 readability and clarity of the code. String methods may be applied to a text block: """


Text blocks may be used as a method argument: (""" String together = str + " and " + tb + "." For example, text blocks may be intermixed with string literals in a string concatentation 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. The object produced from a text block is a with the same characteristics as a traditional double quoted string. Text blocks eliminate most of these obstructions, allowing you to embed code snippets and text sequences more or less as-is.Ī text block is an alternative form of Java string representation that can be used anywhere a traditional double quoted string literal can be used. In earlier releases of the JDK, embedding multi-line code snippets required a tangled mess of explicit line terminators, string concatenations, and delimiters. IntroductionĪ 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. This guide assembles practical usage advice for text blocks, along with some style guidelines. While the JEP explains the feature in great detail, it's not always apparent how the feature can and should be used. JEP 355 introduces text blocks into Java SE 13 as a Preview feature.
