Forums Register Login

difficulty in understanding backslash?

+Pie Number of slices to send: Send
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" ?
+Pie Number of slices to send: Send
 

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.

+Pie Number of slices to send: Send
On my Eclipse console, its giving me some weird square like character. I cant even paste that here...
1
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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'.
+Pie Number of slices to send: Send
 

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'.
+Pie Number of slices to send: Send
 

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

 
+Pie Number of slices to send: Send
 

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.
eat bricks! HA! And here's another one! And a tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 4575 times.
Similar Threads
'\' in String
need help. please. PLEASE!
saving the image path instead of the image itself
backslashes need to be replaced
How do I print a division symbol and parenthesis?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 17:41:13.