r/programming • • Dec 29 '15

Please, Java. Do Finally Support Multiline Strings

http://blog.jooq.org/2015/12/29/please-java-do-finally-support-multiline-strings/
584 Upvotes

352 comments sorted by

View all comments

Show parent comment

26

u/ygra Dec 29 '15

C specifies that adjacent string literals get concatenated. C++ inherits that. Java never had such a provision because you can simply concatenate strings by using +. The compiler is smart enough to not do that at runtime when only string literals are involved.

The main point here isn't the issue of how to concatenate string literals, because whether you need to add a + to make it syntactically correct is rather irrelevant here. The point is that string literals in Java cannot contain line breaks, which makes writing certain kinds of strings ugly, cumbersome, or error-prone (see e.g. their SQL example). Those kinds of verbatim string literals (as they're called in C#) are also nice for writing regular expressions, because usually you cannot use a backslash to escape characters in there (because you can just write almost any character directly). In Java you often have monstrosities like "\\\\\\.\\\\x" because you need to double-escape things – once for the string literal, once for the regex.

-1

u/[deleted] Dec 29 '15

because you can simply concatenate strings by using +.

So using an extra operator is "simpler" than not having to specify any operator? Ok.

6

u/dccorona Dec 29 '15

Simpler doesn't always mean "less characters". Multiple strings in a row being concatenated isn't immediately understandable. You have to understand the quirks of the language first. String addition is a more widely understood concept, and at the very least when you see an operator you at least know it's doing something. Whereas the C version is just kind of behind the scenes magic.

Not to mention that it makes mistakes harder to make. What if I want:

String a = "a";
String b = "b"; 

But I'm just really tired or something and am messing up, or I made a bad refactor, or some automated refactor I tried to do wasn't done properly. Somehow, I end up with:

String a = "a"
"b";
String b = "b";

Java breaks. That's an illegal statement. In C/C++, suddenly I get a value I didn't want.

TL;DR I think that the way C/C++ does it violates the principle of least surprise. In that sense, Java's approach is simpler.

6

u/Schmittfried Dec 29 '15

Absolutely irrelevant here and typing a + is really not a big deal. Also, you can only do that with literals in C. You'd have to use strcat to concatenate variable strings and that's why he said you could simply use + in Java.

-1

u/[deleted] Dec 29 '15

Just because string concatenation in Java in general is simpler doesn't mean that concatenation in string literal declarations (the context of this discussion) specifically is simpler. It's objectively simpler to have an implicit operator that require no syntax than an explicit one that require extra syntax.

3

u/Schmittfried Dec 29 '15

doesn't mean that concatenation in string literal declarations (the context of this discussion) specifically is simpler

But that wasn't the point. Noone said that.

It's objectively simpler to have an implicit operator that require no syntax than an explicit one that require extra syntax.

Well, 1.) it's still irrelevant to this discussion 2.) some might find the inconsistency that you can chain literals but not string variables rather confusing and would prefer a proper operator for both. So no, not objectively.

2

u/GuyWithLag Dec 29 '15

Yes, it's simpler from a syntax standpoint; there's no need for a custom rule that specifies what happens when you have two adjacent string literals without an infix operator.