Why doesn't Java have a way to specify unescaped string literals?

In C #, if you want the string to be taken literally, i.e. ignored escape characters, you can use:

string myString = @"sadasd/asdaljsdl"; 

However, there is no equivalent in Java. Is there a reason Java didn't include something like this?

Edit:

After reviewing some of the answers and thinking about this, I really ask:
Are there any good arguments against adding this syntax to Java? Any negative that I just donโ€™t see?

+6
java c # language-comparisons
source share
8 answers

Java has always amazed me as a minimalist language - I would suggest that since shorthand lines are not necessary (for example, properties, for example), they were not included.

For example, in C # there are many quick ways to do something like properties:

 public int Foo { get; set; } 

and shorthand lines:

 String bar = @"some string"; 

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

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

and the lines should be escaped:

 String bar = "some\nstring"; 

I think this is because C # and Java have different design goals. C # is evolving rapidly, and many functions are constantly being added, but most of them are usually syntactic sugar. Java, on the other hand, is simplicity and ease of understanding. Many of the reasons Java was created were primarily reactions to the complexity of C ++ syntax.

+5
source share

The "why" questions seem funny to me. C # is a newer language and is trying to improve what is seen as flaws in other languages โ€‹โ€‹such as Java. A simple reason for asking why is that the Java standard does not define the @ operator, such as in C #.

+2
source share

As stated above, in most cases when you want to avoid characters, for regular expressions. In this case, use: Pattern.quote ()

+2
source share

I think one of the reasons is that regular expressions (which are the main reason for these types of String literals) are not part of the Java platform before Java 1.4 (if I remember correctly). There simply was no need for this when the language was defined.

+1
source share

Java (unfortunately) has nothing of the kind, 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 C # feature when I was working on .NET. This was especially useful for cut and pasted SQL queries.

+1
source share

I think this question is: "Why is java not indent sensitive like Python?" The syntax mentioned is sugar, but it is redundant.

0
source share

I'm not sure why, but you can do this by avoiding the escape character. Since all escape characters are preceded by a backslash, by adding a double backslash, you can effectively cancel the escape character. for example, "\ now" will create a new line, then the letters "ow", but "\ now" will call "\ now"

0
source share

You should find that your IDE is dealing with this problem. If you are in the middle of a string text and paste the source text into it, it should avoid the text for you.

PERL has a wider set of ways to set string literals, and sometimes Java supports them.;)

0
source share

All Articles