This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
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


Win a copy of Java 8 in Action this week in the Java 8 forum!
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: 682

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: 2247

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: 7081
    
  16

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?
Artlicles 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: 4244
    
    7

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: 7081
    
  16

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: 7081
    
  16

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: 60071
    
  65

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: 13884
    
  10

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: http://aspose.com/file-tools
 
subject: why java doesnt allow "\" ?
 
Similar Threads
newInstance() doubt
Java in XSLT
JSP Tag that contains result of another
setting any object as property in java bean
Apache Commons FileUpload isMultipartContent() Deprecated.