

If the feature was immediately made part of the Java SE standard, it would be a lot harder to make changes. The feedback may lead to changes in the feature, or even the feature being deleted if necessary. The benefit of this is that users can test the feature and provide feedback. Text blocks are provided as a preview feature, which means they are not included in the relevant Java Language Specification. It is thoroughly recommended to read this. This provides more detail of the feature with a variety of examples. Jim Laskey and Stuart Marks have written a very useful Programmer's Guide to Text Blocks. (If you want to you can escape two or three of the quotes or the second or the third just escaping the first probably makes the most sense, though). Regular Java language escape sequences work as expected, and double quotes do not need to be escaped unless you want a sequence of three, in which case, at least one of them must be escaped. This can be done explicitly using the indent() method of String. However, it is then not possible to control incidental space. If this is not required, the closing quotes can be placed directly after the end of the text.

In the above example, the closing quotes are placed on their own line in addition to controlling the amount of incidental space eliminated, this will also add a newline to the end of the generated string. Moving the quotes further to the right has no effect and will not add more indentation spaces.

Moving it all the way to the left-hand margin would result in all indentation spaces being retained. If we moved it two spaces to the left, we would add two indentation spaces to each line of the string. If we moved the closing quotes closer to the left-hand margin, this would change the number of indentation spaces removed. If this is not required, the closing """ can be placed directly after the tag. Note that this will include a newline at the end of the string. In this case, the compiler will remove the indentation spaces resulting in a string like this: In the example above, the closing quotes are aligned with the indentation of the HTML text. The positioning of the closing quotes is significant because it determines how incidental whitespace is handled.

There are a couple of subtleties that you need to be aware of in using text blocks. TextBlock.java:3: error: illegal text block open delimiter sequence, missing line terminatorĪ simple piece of HTML can now be assigned to a String like this: String htmlBlock = """ This would generate the following errors: TextBlock.java:3: error: illegal text block open delimiter sequence, missing line terminator The opening quotes must be followed by a line terminator text blocks cannot be used on a single line. Text blocks can be used anywhere that a string literal can be used in Java with no difference to the compiled code. There is nothing magical about the end result, which is still a normal object. Anything between them is interpreted as a part of the string, including newlines. A text block starts with a triple double-quote and is terminated with the same. Text blocks are a different solution to the same challenge. In JDK 12, there was a proposal for raw-string literals ( JEP 326), but the feedback from the early access builds of the JDK was such that the decision was made to remove it before release. This leads to workarounds, like including \n where line breaks are required or concatenating strings and newlines, but these do not lend themselves to clear code. A string starts with a double-quote and ends with a double-quote, which is fine, except that the string cannot span more than one line. Java has always suffered a little from the way strings are defined. Let's start with a change to the language syntax in the form of text blocks ( JEP 355). This is the case with JDK 13, which includes only five JDK Enhancement Proposals (JEPs) and 76 new core library elements (of which nearly half are simple additions to the java.io package) One thing that is obvious, is that, whilst the overall rate of change for the Java platform has increased, individual releases will have less new features. We are now well into the new six-month release cadence of the JDK, and with the release of JDK 13, it is clearly working successfully.
