aspose file tools
The moose likes Java in General and the fly likes why java doesnt allow Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "why java doesnt allow "\" ?" Watch "why java doesnt allow "\" ?" New topic
Author

why java doesnt allow "\" ?

naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 675

in java

where as


The Only way to learn is ...........do!
Visit my blog http://inaved-momin.blogspot.com/
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2212

The backslash is a special character. A character preceeded by a "\" is a escape sequence. Please take a look here for more information.


Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 4734
    
    7

Roberto Perillo wrote:The backslash is a special character. A character preceeded by a "\" is a escape sequence.

Actually, the problem is that backslash is a special character for both Strings and regexes, and each define their own escape sequences.

\d{10} is certainly a valid regex, but the trouble is that regexes are also Strings, and \d is NOT a valid escape sequence for a String.

Therefore, you have to double up the \ so that the regex processor gets to see it, viz:
String regex = "\\d{10}";

Personally, I think they would have saved an awful lot of bother by choosing a different escape character for Strings.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 5807
    
    5

Winston Gutkowski wrote:
Roberto Perillo wrote:The backslash is a special character. A character preceeded by a "\" is a escape sequence.

Actually, the problem is that backslash is a special character for both Strings and regexes, and each define their own escape sequences.

\d{10} is certainly a valid regex, but the trouble is that regexes are also Strings, and \d is NOT a valid escape sequence for a String.

Therefore, you have to double up the \ so that the regex processor gets to see it, viz:
String regex = "\\d{10}";


Actually, it's a special character in String literals, that is, quoted strings in your source code.

So in the above, the compiler sees "\\" and says, "Oh, the first \ is escaping the second one, so I will create a String with the characters '\', 'd', '{', '1', '0', '}' in it." If we were, say, reading the String from a file, then we could simply put

\d{10}

in the file, and something like


and end up with the same results--that is, in both cases, the String contains the characters:
\d{10}

Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 3786
    
    1

Winston Gutkowski wrote:Personally, I think they would have saved an awful lot of bother by choosing a different escape character for Strings.

I quite like the C# feature of being able to prefix string literals with @ to disable escaping - it comes in very handy for regular expressions and Windows file paths. E.g.:
String regex = @"\d{10}";
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 4734
    
    7

Jeff Verdegan wrote:Actually, it's a special character in String literals...

Yes. Quite right.

Winston
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 5807
    
    5

Matthew Brown wrote:
Winston Gutkowski wrote:Personally, I think they would have saved an awful lot of bother by choosing a different escape character for Strings.

I quite like the C# feature of being able to prefix string literals with @ to disable escaping - it comes in very handy for regular expressions and Windows file paths. E.g.:
String regex = @"\d{10}";


That is handy, and, IMHO, a better solution than picking some other escape character (which could then end up needing to be double escaped in some other context).
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 4734
    
    7

Matthew Brown wrote:I quite like the C# feature of being able to prefix string literals with @ to disable escaping - it comes in very handy for regular expressions and Windows file paths.

Indeed. I wonder why they haven't included it? Perhaps there's an aversion to accepting anything from Microsoft.

Winston
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56163
    
  13

Because outside of Windows file paths, \ is rarely used.


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 5807
    
    5

Bear Bibeault wrote:Because outside of Windows file paths, \ is rarely used.


Hmm... It seems to me there's some other context in which we regularly need that character to express something, but I just can't remember what it is...
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12907
    
    3

The meaning of \ as an escape character in string literals is something that Java has inherited from C and C++.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: jrebel
 
subject: why java doesnt allow "\" ?
 
Similar Threads
setting any object as property in java bean
newInstance() doubt
JSP Tag that contains result of another
Java in XSLT
Apache Commons FileUpload isMultipartContent() Deprecated.