Sunday, March 7, 2010

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.  

#!/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:
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.

15 comments:

  1. C# has support for string literals (see here: msdn.microsoft.com/en-us/library/aa691090%28VS.71%29.aspx).
    So, it shouldn't be a big deal to port into Java.
    These literals work mostly OK apart from when you have double-quote characters. Then you have to escape them as "".

    So, your example above would become:
    Console.Out.WriteLine(@"Here is some text, embedded
    ""right in the middle""
    of my C# program.
    ");

    ReplyDelete
  2. When I bought my computer and I didn´t know how to use java graphics, so I decided looking for information in a webside and I found an useful information that helped me a lot.. Now I am interested in to do the best investment and I found a webside very useful and interesting called costa rica investment opportunities , I think it´s a very wonderful site.

    ReplyDelete
  3. I'm writing a program that needs to glue together chunks of pre-specified HTML, with the way it glues them together depending on a state variable. If Java had a JSP (Java Server Pages) compiler that could translate JSP notation to Java, then I could write each chunk as a separate JSP file, compile it to a class or method, and refer to that in my glueing code. That would be almost as good as here-documents: perhaps better, because the glueing code wouldn't get clogged up with large pieces of verbatim text.

    So I suggest that a stand-alone JSP-to-Java compiler would be a good substitute for Java here-documents.

    ReplyDelete
  4. I am not really sure about the original content of the post.

    ReplyDelete
  5. Actually I have very poor knowledge about it but I can say this blog is really good and awesome.

    ReplyDelete
  6. This is a handy way of embedded string literals that would normally have to be escaped right...

    ReplyDelete
  7. XML and Unit test are seriously taken in its own way..It would be better if you give the best effort.

    ReplyDelete
  8. This is a handy way of embedded string literals, HCG Blue Drops, the Faster and Natural way to lose weight...

    ReplyDelete
  9. Thanks for the amazing article here. I was searching for something like that for quite a long time and at last I have found it here. Your blog is better than others because of useful and meaningful posts. Keep posting them in the future too, I will be waiting for thatWe have many brand



    natural supplements

    ReplyDelete
  10. thanks for the code . It will work for me in another way . thanks for share.

    ReplyDelete
  11. 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?

    ReplyDelete
  12. This language structure is supported in many newer languages, such as Ruby. Python, PHP and Perl.

    ReplyDelete
  13. This is great for sharing this parts o JAVA programming

    ReplyDelete