19

I've read that multiline string literals were proposed to be added in Java 7.

Although I can't find any documentation saying definitely that they have been. I'd like to know if they are, because this is something I'd consider switching versions for.

João Silva
  • 89,303
  • 29
  • 152
  • 158
Mike
  • 58,961
  • 76
  • 175
  • 221
  • So @Mike, what's the question again? – mauris Jan 03 '11 at 00:42
  • 1
    There's a [proposal from 2008](http://www.jroller.com/scolebourne/entry/java_7_multi_line_string), but nothing in the [feature list](http://openjdk.java.net/projects/jdk7/features/). There's a [library implementation](http://blog.efftinge.de/2008/10/multi-line-string-literals-in-java.html). – moinudin Jan 03 '11 at 00:43
  • Why do you need multi line strings, if you really need them load text files etc. – mP. Jan 03 '11 at 00:43
  • 5
    @mp I'm hoping to avoid discussing why. let's just assume I have a good reason for it. – Mike Jan 03 '11 at 00:44
  • 1
    possible duplicate of [Java multiline string](http://stackoverflow.com/questions/878573/java-multiline-string) – Jason C Mar 28 '14 at 20:23

4 Answers4

17

Multiline string literals are not going to be added to JDK 7. You can check Project Coin's homepage for a list of language changes.

However, you can use Scala, which does support multiline string literals using triple quotes:

var s = """Hello
      World"""
João Silva
  • 89,303
  • 29
  • 152
  • 158
  • Because it also targets the JVM. – João Silva Jan 03 '11 at 00:54
  • @Mike I would consider Scala "the evolution" of Java in terms of language design that is not held back by the previous versions of Java although it is still shackled with the general design of the JVM/CLR "OOP" paradigms. (E.g. Scala is to Java as C#5.0+ will be to C#1.0 :-) –  Jan 03 '11 at 01:20
  • The problem with scala in this context is losing the simplicity of all source and files within one place, which is the win of heredocs. – WestCoastProjects May 28 '13 at 01:08
  • This is of course of topic of the original question but I would suggest that Groovy is now the evolution of Java and it supports multiline strings using either three double-quotes or single-quotes. – Jack Holt Jun 18 '14 at 19:51
  • Well since we branch out in this question I'd say Kotlin does a good job at this. – E. Sundin Jul 20 '17 at 11:56
5

Multiline strings were not added into Java (even as of Java 8, the newest current version), and probably will never be added to Java. However, you can add multiple strings together like so:

String greeting = "Hello " + 
    "world! " + 
    "This is a multiline string.";

Or, if you want the multiline line breaks to actually start a new line, insert "\n" to the end of each line.

hyper-neutrino
  • 5,272
  • 2
  • 29
  • 50
2

Multiline strings are supported in Java since JDK 13. They are called text blocks:

String html = """
          <html>
              <body>
                  <p>Hello, world</p>
              </body>
          </html>
          """;

Note, this is a preview feature. But I hope it will become a permanent feature in one of the next releases (JDK 14-15).

ZhekaKozlov
  • 36,558
  • 20
  • 126
  • 155
1

Following Java's coding conventions Strings should be concatenated like:

String str = "Long text line " 
             + "more long text.";

Make sure the + operator always begins the next line for readability.
See: Code Conventions for the Java Programming Language: 4. Indentation

brasofilo
  • 25,496
  • 15
  • 91
  • 179
bradylange
  • 358
  • 4
  • 6