Multi-line Strings in Java
Posted 2005 Jun 01
One big annoyance with Java is the lack of any way to use multi-line strings like a HERE document. No doubt Java is often used to query a database, yet a developer has to take a SQL statement and re-write it with “s and +s, like so:
<pre><code> select sysdate from dual </code>becomes
<pre><code> String sql = "select sysdate "+ "from dual"; </code>Don’t forget the extra spaces! This is fine until your SQL statement becomes larger (which it will). While searching for a better way to handle this, I came upon the bug report at Sun for this issue. Here is Sun’s response to this issue:
This is yet another request for a syntactic sugar to save some user from typing. It’s not worth it.
Wow. Thanks for the understanding… No wonder people are looking for alternatives. Even Groovy supports Python-style multi-line strings! One possibility I found is Java+, a pre-processor. It integrates well with Ant and does its job, but source-code management becomes complicated—especially if you’re using an IDE.
If Sun finally broke down and added printf, the least it could do is consider adding multi-line strings.