Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

difficulty in understanding backslash?

 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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" ?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On my Eclipse console, its giving me some weird square like character. I cant even paste that here...
 
Paul Clapham
Sheriff
Posts: 21315
32
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21315
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic