26

Why doesn't Java support multi-line strings? I know they dont, but I dont know why. Is there a good reason? Several other languages have this capability, even older ones, so why doesnt Java? As far as I know (not very far) it shouldnt be too had to add this functionality to your lexers/compilers.

Edit: For clarification, I dont mean a string with a newline character in it. I mean something like this:

String s = "Hello
            World";

Edit2: I dont know why people thought I was asking for opinions, I most certainly am not. I specifically asked for good reasons. I suppose I need to explicitly say based on facts as well?

David says Reinstate Monica
  • 19,209
  • 22
  • 79
  • 122
  • 4
    Barring a Sun/Oracle employee coming here to answer the question, answers will be entirely opinion-based and conjecture. – asteri Mar 28 '14 at 20:10
  • 4
    @JeffGohlke Sometimes (often) they have good reasons for doing/not doing something. Perhaps someone actually knows, understands, and can explain it. This is why Im asking if there is a *good* reason, not just an arbitrary reason – David says Reinstate Monica Mar 28 '14 at 20:12
  • @Dgrin91 Did `String a = "This is a string, and " + "this is on the next line of my source file";` not work for you when you pressed "enter" after typing `+`? – Jason C Mar 28 '14 at 20:13
  • 1
    By the way, see also the accepted answer at: http://stackoverflow.com/questions/878573/java-multiline-string?rq=1 – Jason C Mar 28 '14 at 20:16
  • While Java does not support multiline strings, it does have a very simple string concatenation operator: multilineString = "I am the first line of a multiline string.\n" + "I am the second line of the same string." This is almost as good as straight multiline strings, while making control characters such as the newline `\n` explicit, and additionally facilitates the idea that whitespace does not matter outside of strings. The latter two considerations are important to minimizing ambiguity. – Warren Dew Mar 28 '14 at 20:17
  • @JasonC that answer is an over 3 year old proposal for java 7. We are already in Java 8 and still no multilines. – David says Reinstate Monica Mar 28 '14 at 20:17
  • 1
    @Dgrin91 Then it seems you did not read the blog post linked to in the answer, which not only discusses that, but would have taken you more than 45 seconds to read. If you are not willing to make the effort to perform that one extra mouse click, then we cannot be expected to make the effort to essentially retype that and [similar other articles](https://www.google.com/search?q=java+multiline+strings) for you here. – Jason C Mar 28 '14 at 20:18
  • @Dgrin91 The answer hasn't been incorporated into Java because multiline strings are a bad idea for Java. See the reasons in my earlier comment - sorry about the formatting, but I didn't get in before this question was prematurely closed. – Warren Dew Mar 28 '14 at 20:26
  • 4
    The String you posted. Is that `Hello\nWorld` or `Hello\n World`. This is the problem. If it's one or the other, how would you represent the other one? Java wanted to be more well defined than some of these hipster languages doing this. It works a little bit better in python because indentation is part of syntax, so it's less ambiguous when you don't have an arbitrary amount of whitespace that could be syntax. – Cruncher Mar 28 '14 at 20:27
  • How should the line endings be encoded? (`\n`? `\r\n`?) Should the spaces before the first word of each line be ignored? In strongly-typed languages like Java, you want control over everything, so it would be strange if the compiler decided this for you. – Dalmas Mar 28 '14 at 20:51
  • I ask me every day about it – deFreitas Aug 25 '16 at 03:18

0 Answers0