Ruby Programmer/Developer

George Armhold's Blog

Data-driven webapps built with Ruby, Java & Wicket

Why No "Here" Documents for Java?

Many languages support the notion of  ”here documents”.  This is a handy way of embedded string literals that would normally have to be escaped right into your program text.  

1
2
3
4
5
6
7
8
#!/bin/sh
cat << "EOF"
Here is some text, embedded

   "right in the middle"

of my Bourne shell script.
EOF

Having been a standard feature in the Unix shells for decades, this language structure is supported in many newer languages, such as Ruby. Python, PHP and Perl.  But not Java.  In Java, this program would look like:

1
System.out.println("Here is some text, embedded\n" +" \"right in the middle\"\n" +"of my Java program.\n");

As you can see, the quoting and escaping required to reproduce this text is pretty ugly.

Wouldn’t it be nice if we could do this kind of thing in Java too?  It would be great for things like XML and Unit Tests.   How did we get to 2010 without having this as a standard language feature?  Has it ever come up in a JSR?   I haven’t had much luck in digging up any information (the term “here document” is remarkably hard to search on.) If you know something about this, please leave a comment.