This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes difficulty in understanding backslash? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "difficulty in understanding backslash?" Watch "difficulty in understanding backslash?" New topic
Author

difficulty in understanding backslash?

naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 688

In Java backslash "\" are used with character for special meaning like for eg: "\n", "\t" etc
but I know "\n" is use for newline but what is the use of "\10"
I guess backslash's are use to give a special meaning to a character like "\n" for newline but what's special in "\10" ?


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

Joined: Jan 03, 2004
Posts: 6109
    
    6

naved momin wrote:In Java backslash "\" are used with character for special meaning like for eg: "\n", "\t" etc
but I know "\n" is use for newline but what is the use of "\10"
I guess backslash's are use to give a special meaning to a character like "\n" for newline but what's special in "\10" ?


The backslash has different meanings in different contexts. In a Java String literal, it means that it plus the next character combine to form a single "special" charcter. For instance "\n" means "newline" (ASCII 0x0A or 0x0D, I forget which) and "\\" means "literal backslash."

Some combinations don't have any special meaning. For instance, the "\1" in your "\10" above doesn't mean anything special. I don't recall if Java just ignores the backslash in these cases, so that "\1" just becomes "1" and "\10" just becomes "10", or if it's a syntax error.

Praveen Kumar M K
Ranch Hand

Joined: Jul 03, 2011
Posts: 256
On my Eclipse console, its giving me some weird square like character. I cant even paste that here...
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

My test code fragment

output 8. So I guessed that octal (base 8) representations might be involved. So I tried

and sure enough I got a compiler error ("Invalid character constant"). So the backslash must mean that the following digits are to be interpreted as octal digits. But like Jeff I was completely unfamiliar with this usage.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Paul Clapham wrote:My test code fragment

output 8. So I guessed that octal (base 8) representations might be involved. So I tried

and sure enough I got a compiler error ("Invalid character constant"). So the backslash must mean that the following digits are to be interpreted as octal digits. But like Jeff I was completely unfamiliar with this usage.


Which goes back to what I initially said: "different meanings in different contexts". The OP asked about "\10", which, it seems, is different than '\10'.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Jeff Verdegan wrote:The OP asked about "\10", which, it seems, is different than '\10'.


Is it? My test code

outputs 1. And then

outputs 8. So to me it looks like "\10" acts exactly like a 1-character string containing '\10'.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Paul Clapham wrote:
Jeff Verdegan wrote:The OP asked about "\10", which, it seems, is different than '\10'.


Is it? My test code

outputs 1. And then

outputs 8. So to me it looks like "\10" acts exactly like a 1-character string containing '\10'.


I stand corrected. I thought I knew for certain that "\<digits>" was nothing special in a String. Guess I should have double checked..


EDIT...

From JLS 3.10.6 Escape Sequences for Character and String Literals (Note the "and String"--I always thought they were slightly different.)

OctalEscape:
\ OctalDigit
\ OctalDigit OctalDigit
\ ZeroToThree OctalDigit OctalDigit
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38064
    
  22
Praveen Kumar M K wrote:On my Eclipse console, its giving me some weird square like character. I cant even paste that here...
That is because \10 (=\u0008) is a control character. Look here to find out what it means.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: difficulty in understanding backslash?
 
Similar Threads
How do I print a division symbol and parenthesis?
backslashes need to be replaced
'\' in String
need help. please. PLEASE!
saving the image path instead of the image itself