JEP 326‘s “Summary” provides an overview of the proposed Java raw string literals: “A raw string literal can span multiple lines of source code and does not interpret escape sequences, such as \n, or Unicode escapes, of the form \uXXXX.” The “Motivations” section of this JEP adds, “This JEP proposes a new kind of literal, a raw string literal, which sets aside both Java escapes and Java line terminator specifications, to provide character sequences that under many circumstances are more readable and maintainable than the existing traditional string literal.” JEP 326 does not introduce interpolation and, in fact, rules it out in its “Non-Goals” section: “Raw string literals do not directly support string interpolation. Interpolation may be considered in a future JEP.”
The current version of JEP 326 states that Java’s raw string literals will be denoted via the use of the “backtick” character (
`), which is also described in the JEP as
\u0060 (Unicode “Grave Accent”), “backquote“, and “accent grave“. I don’t show any examples of the proposed syntax because the JEP already does a nice job of listing these proposed raw string literal examples alongside examples of traditional Java code needed to implement the same thing. This makes it easy to compare the required current syntax to what would be needed in the future to accomplish the same thing if raw string literals are supported.
Support for raw string literals in Java will provide nice convenience for Java developers wishing to write more readable code to support use cases like those described in the JEP. It will provide similar advantages to libraries and even to the JDK code. The core-lib-devs mailing list post “Raw String Literal Library Support” [JDK-8196005] starts a “discussion with regards to RSL library support.” (The context of “library support” in this case is the JDK and RSL stands for Raw String Literal.).
In the referenced post Raw String Literal Library Support, Jim Laskey provides a list of methods to potentially add to String to take advantage of raw string literals. These ideas for kicking off discussion include “line support”, enhancements to “trim” methods, “margin management”, and “escape management”. Some of these are facilitated by RSL while others are necessitated by RSL. The cited post provides multiple examples of each of these.
Although JEP 326 is still just a “Candidate” and is not associated with a particular release of Java, recent work on it and recent discussion in mailing list posts seeking input related to it lead me to be cautiously optimistic that we’ll see multi-line Java strings and other raw string literals coming to Java in a future release.