6

In C#, if you want a String to be taken literally, i.e. ignore escape characters, you can use:

string myString = @"sadasd/asdaljsdl";

However there is no equivalent in Java. Is there any reason Java has not included something similar?

Edit:

After reviewing some answers and thinking about it, what I'm really asking is:
Is there any compelling argument against adding this syntax to Java? Some negative to it, that I'm just not seeing?

shsteimer
  • 28,436
  • 30
  • 79
  • 95

8 Answers8

5

Java has always struck me as a minimalist language - I would imagine that since verbatim strings are not a necessity (like properties for instance) they were not included.

For instance in C# there are many quick ways to do thing like properties:

public int Foo { get; set; }

and verbatim strings:

String bar = @"some
string";

Java tends to avoid as much syntax-sugar as possible. If you want getters and setters for a field you must do this:

private int foo;

public int getFoo() { return this.foo; }
public int setFoo(int foo) { this.foo = foo; }

and strings must be escaped:

String bar = "some\nstring";

I think it is because in a lot of ways C# and Java have different design goals. C# is rapidly developed with many features being constantly added but most of which tend to be syntax sugar. Java on the other hand is about simplicity and ease of understanding. A lot of the reasons that Java was created in the first place were reactions against C++'s complexity of syntax.

Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
  • 1
    Java 7 will have properties also :) – unorsk Mar 08 '09 at 16:51
  • 1
    It's a bit hard to characterize a language where you can write "new HashMap – Steve B. Mar 08 '09 at 17:02
  • 4
    Heh, Java is only "minimalist" in a very specific sense. I see what you mean, and I agree. But at the same time, it is also an incredibly big and bloated language, trying to be everything for everyone. :) But yes, it seems like Java dislikes shortcuts. – jalf Mar 08 '09 at 17:03
  • I'm just still in shock that Sun's explanation for not including unsigned types was something like "most people don't know what those are". – Spencer Ruport Mar 08 '09 at 17:41
  • @jalf - well put "dislikes shortcuts" is moe accurate than "minimalist" :) – Andrew Hare Mar 08 '09 at 17:57
  • Spencer Ruport, Gosling's explained the lack of unsigned arithmetic using an example that most people got wrong. Funnily enough the example does the same thing for signed, two's-complement arithmetic too. Most people should be using arbitrary sized integers. – Tom Hawtin - tackline Mar 09 '09 at 11:59
  • Java the ancient seems to progressing about adding Raw String Literals http://openjdk.java.net/jeps/326 – GOXR3PLUS Sep 11 '18 at 09:24
2

I find it funny "why" questions. C# is a newer language, and tries to improve in what is seen as shortcomings in other languages such as Java. The simple reason for the "why" question is - the Java standard does not define the @ operator such as in C#.

Otávio Décio
  • 73,752
  • 17
  • 161
  • 228
  • Just a nitpick: @ isn't an operator. It's just part of the syntax for a verbatim string literal. – Jon Skeet Mar 08 '09 at 16:51
  • C++ is even older than Java but it supports raw string literal long time ago. Java spec is also updated but it's still far from other languages even after the updates – phuclv Apr 15 '16 at 08:06
2

Like said, mostly when you want to escape characters is for regexes. In that case use: Pattern.quote()

Community
  • 1
  • 1
nxadm
  • 2,079
  • 12
  • 17
1

I think one of the reasons is that regular expressions (which are a major reason for these kind of String literals) where not part of the Java platform until Java 1.4 (if I remember correctly). There simply wasn't so much of a need for this, when the language was defined.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
1

Java (unfortunately) doesn't have anything like this, but Groovy does:

assert '''hello,
world''' == 'hello,\nworld'
//triple-quotes for multi-line strings, adds '\n' regardless of host system
assert 'hello, \
world' == 'hello, world' //backslash joins lines within string

I really liked this feature of C# back when I did some .NET work. It was especially helpful for cut and pasted SQL queries.

Jeff Olson
  • 6,323
  • 2
  • 22
  • 26
  • This is what I was looking for. But the OP asks about escape characters; Groovy ignores those with slashy strings, not triple quotes. – Noumenon Oct 03 '15 at 22:34
0

I think this question is like: "Why java is not indentation-sensitive like Python?" Mentioned syntax is a sugar, but it is redundant (superfluous).

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
unorsk
  • 9,371
  • 10
  • 36
  • 42
0

I am not sure on the why, but you can do it by escaping the escape character. Since all escape characters are preceded by a backslash, by inserting a double backslash you can effectively cancel the escape character. e.g. "\now" will produce a newline then the letters "ow" but "\now" will produce "\now"

Benjamin Lee
  • 1,160
  • 3
  • 13
  • 18
0

You should find your IDE handles the problem for you. If you are in the middle of a String and copy-paste raw text into it, it should escape the text for you.

PERL has a wider variety of ways to set String literals and sometimes wish Java supported these as well. ;)

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130