views:

594

answers:

8

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?

+5  A: 

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
Java 7 will have properties also :)
Andrew Dashin
It's a bit hard to characterize a language where you can write "new HashMap<Integer,ArrayList<? extends .. " as minimalist :)
Steve B.
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
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
@jalf - well put "dislikes shortcuts" is moe accurate than "minimalist" :)
Andrew Hare
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
+2  A: 

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
Just a nitpick: @ isn't an operator. It's just part of the syntax for a verbatim string literal.
Jon Skeet
Thanks for the clarification, Jon.
Otávio Décio
A: 

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

Andrew Dashin
+1  A: 

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
OTOH, inlined SQL was more common in the olden days.
Tom Hawtin - tackline
@Tom: yes, but encouraging SQL injection attacks wasn't high on the priority list ;-) PreparedStatements don't have a problem with escaping.
Joachim Sauer
A: 

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
Have you read the question?
Jan Jungnickel
A: 

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
Off-topic: it's Perl, not PERL.
+2  A: 

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

A: 

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