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 | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
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: 692

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: 2267
    
    3

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: 8244
    
  23

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?
Articles by Winston can be found here
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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: 4467
    
    8

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: 8244
    
  23

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

Yes. Quite right.

Winston
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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: 8244
    
  23

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: 61654
    
  67

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


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14350
    
  22

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 8 API documentation
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: why java doesnt allow "\" ?