Core Java

Please, Java. Do Finally Support Multiline String Literals

I understand the idea of Java-the-language being rather hard to maintain in a backwards-compatible way. I understand the idea of JDK API, such as the collections, to be rather tough not to break. Yes.

I don’t understand why Java still doesn’t have multiline string literals.

How often do you write JDBC code (or whatever other external language or markup, say, JSON or XML you want to embed in Java) like this?

try (PreparedStatement s = connection.prepareStatement(
    "SELECT * "
  + "FROM my_table "
  + "WHERE a = b "
)) {
    ...
}

What’s the issue?

  • Syntax correctness, i.e. don’t forget to add a whitespace at the end of each line
  • Style in host language vs style in external language, sure the above code looks “nicely” formatted in Java, but it’s not formatted for the consuming server side
  • SQL injection, didn’t we teach our juniors not to perform this kind of string concatenation in SQL, to prevent SQL injection? Sure, the above is still safe, but what keeps a less experienced maintainer from embedding, accidentally, user input?

Today, I was working with some code written in Xtend, a very interesting language that compiles into Java source code. Xtend is exceptionally useful for templating (e.g. for generating jOOQ’s Record1Record22 API). I noticed another very nice feature of multi line strings:

The lack of need for escaping!

Multi line strings in Xtend are terminated by triple-apostrophes. E.g.

// Xtend
val regex = '''import java\.lang\.AutoCloseable;'''

Yes, the above is a valid Java regular expression. I’m escaping the dots when matching imports of the AutoCloseable type. I don’t have to do this tedious double-escaping that I have to do in ordinary strings to tell the Java compiler that the backslash is really a backslash, not Java escaping of the following character:

// Java
String regex = "import java\\.lang\\.AutoCloseable;";

So… Translated to our original SQL example, I would really like to write this, instead:

try (PreparedStatement s = connection.prepareStatement(
    '''SELECT *
       FROM my_table
       WHERE a = b'''
)) {
    ...
}

With a big nice-to-have plus: String interpolation (even PHP has it)!

String tableName = "my_table";
int b = 1;
try (PreparedStatement s = connection.prepareStatement(
    '''SELECT *
       FROM ${tableName}
       WHERE a = ${b}'''
)) {
    ...
}

Small but very effective improvement

This would be a very small (in terms of language complexity budget: Just one new token) but very effective improvement for all of us out there who are embedding an external language (SQL, XML, XPath, Regex, you name it) in Java. We do that a lot. And we hate it.

It doesn’t have to be as powerful as Xtend’s multiline string literals (which really rock with their whitespace management for formatting, and templating expressions). But it would be a start.

Please, make this a New Year’s resolution! :)

Lukas Eder

Lukas is a Java and SQL enthusiast developer. He created the Data Geekery GmbH. He is the creator of jOOQ, a comprehensive SQL library for Java, and he is blogging mostly about these three topics: Java, SQL and jOOQ.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

13 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
amr enew
amr enew
8 years ago

With a big nice-to-have plus: String interpolation (even PHP has it)!

>>what about string format or \s in java

Lukas Eder
8 years ago
Reply to  amr enew

Well, I can also write a parser in ANTLR and read external files. There’s always a way to achieve a goal. But this article is about convenience, which leads to developer productivity. In other words, let Java be a bit less of a “Blub language” (http://www.paulgraham.com/avg.html)

amr enew
amr enew
8 years ago
Reply to  Lukas Eder

swift is open source and will be cross platform
so i want to know who is responsible to add languages like groovy to work in jvm ?

Lukas Eder
8 years ago
Reply to  amr enew

Why don’t YOU do it?

amr enew
amr enew
8 years ago
Reply to  Lukas Eder

is there any tutorial to add languages to jvm ?

amr enew
amr enew
8 years ago
Reply to  amr enew

i programming in swift language
in swift and objective-c there is a great feature called extensions
is there is something similar in java?
extension example:
String{
class func isEmailFormat -Bool{
//some code
}
}

var foo = “name@domain”
var isEmail = foo.isEmailFormat()

Lukas Eder
8 years ago
Reply to  amr enew

No, Java doesn’t have extension methods.

Georgian Grec
Georgian Grec
8 years ago

Well… someone could actually make an Eclipse plugin (for example) to “translate” these multiline literals whilst building the workspace. :-)

Lukas Eder
8 years ago
Reply to  Georgian Grec

They certainly could, but before you do that, why not just use another language that supports them. Like… virtually any other language (Scala, Groovy, Xtend, Kotlin, etc.)

mnkartik
8 years ago

Totally agree with Lukas.

It would be a real plus to have such feature. I myself have stumbled upon few of the scenarios to have json string in as formatted way directly inside the java code, which becomes tedious enough when the json string is pretty lengthy. Hence forth got to put that in another file and load or minify in a one liner but with tedious escaping of the spl characters.

Salvatore
Salvatore
8 years ago

https://github.com/benelog/multiline/wiki/Non-Maven-Java-project-with-Eclipse

import org.adrianwalker.multilinestring.Multiline;

public class MailTemplate {

/**

Hello,

You are receiving this email because
you have subscribed to our newsletter.
….

*/

@Multiline private static String msg;

public static String getTemplate() {
return msg;
}
}

Alok Kumar
Alok Kumar
8 years ago

I want to know why finalize method is declared protected not public in Object class?

Alok Kumar
Alok Kumar
8 years ago

What is use of marker interface and user defined marker interface is possible?

Back to top button