One of the most common features in other programming languages is the multi-line String literal. Would it be possible to add this to Java?
Update, 2011-10-31, Just wanted to note that multi-line strings are not in Java 7, nor are they likely to be in Java 8. This blog post is still useful to understand some of the difficulties that would have to be tackled if they were to be included in future.
Update, 2018-01-28: This is now being considered for addition to Java, read more here.
Multi-line String literals
In Java today there is only one form of string literal, supplied in double quotes. Within those double quotes, certain characters have to be escaped.
String basic = "Hello"; String three = "This string\nspans three\nlines"; String welcome = "Hello, My name is \"Stephen\", Hi!";
The first of these two examples is not complex, and would not make use of a multi-line String literal. The other two might be more readable with such a literal.
The standard for defining a multi-line String literal in both Scala and Groovy is three double quotes. This also seems like a sensible choice for Java:
String three = """This string spans three lines""";
This is potentially much more readable, especially with large blocks of text. This form of literal would also avoid the need for escaping:
String welcome = """Hello, My name is "Stephen", Hi!""";
Note that we no longer need to escape the double quotes. This would be especially useful for regular expressions.
Bear in mind that multi-line String literals are fundamentally no different to normal String literals on the key point of the object created. Both would create java.lang.String objects.
Issues
The first issue is the multi-line arrangement. Since all text within the multi-line literal is included, all lines except the first must begin from column zero. This will look odd in a piece of well-formatted Java code:
// what a naive multi-line literal forces us to write public class MyClass { public void doStuff() { String three = """This string spans three lines"""; System.out.println(three); } } // what we'd like to write public class MyClass { public void doStuff() { String three = """This string spans three lines"""; System.out.println(three); } }
One possible solution is to provide a method on String that strips all whitespace after each newline. This could be called directly after the literal. Unfortunately this approach loses some efficiency as the string must be trimmed each time:
// option with trimNewline() public class MyClass { public void doStuff() { String three = """This string spans three lines""".trimNewLine(); System.out.println(three); } }
Another, perhaps better, solution might be to have a syntax variation. If the opening triple quote is followed immediately by a newline, then the position of the first non-space character on the next line represents the column to begin the literal at. (The first newline would not be included in this form of the literal.) Only the space character would be permitted in earlier columns until the end of the literal. This would allow for natural formatting of this kind of string:
// option with columns determined by first line public class MyClass { public void doStuff() { String three = """ This string spans three lines"""; System.out.println(three); } }
One final tricky issue is handling a string containing the triple double quote. The answer is probably to ignore this situation (Scala does this). It is going to be very rare, and it can be worked around using string concatenation.
Summary
Multi-line String literals should be a relatively easy addition to Java (anyone fancy adding it to Kijaro?). The main benefits would be avoiding escaping in regular expressions, and pasting in large blocks of text from other sources.
Overall, I think they would be a valuable addition to Java. But have I missed any obvious issues? Are there any other syntax options that should be considered? Opinions welcome as always :-)