• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

interesting problem in char

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all
it is a killer question asked in a mock.
Which of the following declarations will give a compiler error (Select two correct answers):
a. char c = '\u000a';
b. char c = '\u000b';
c. char c = '\u000c';
d. char c = '\u000d';
answer :
a , d
can any body explain why?
even following also compiles.
char c = '\u000e';
char c = '\u000f';

somebody plz explain why?



 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sony,
They are character literals that represent escape sequences.
'\u000A' is a newline
'\u000D' is return
I would think your choice c would yield an error as well since
'\u000C' is a form feed
Other ones to watch out for '\u0009' tab, '\u0008' backspace, '\u005C' backslash itself, '\u0027' single quote, '\u0022' double quote
Hope this helps!
Ian
 
sony ali
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
Ian Heff
u say about escape sequences . well i m still confused & cant understand why the answer is strange.
well for u'r sake of interest the

char c = '\u000c'; //compiles
but u say it will not compile also.
ithink compiler is our friend thats why i asked him & he told me that it will compile.
thanx in advance
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Are we expected to be familiar with the unicode representation of the common escape sequences as far as preparing for the SCJP
is concerned ? Does this come under any specified objective ?
I hope some of the SCJPs can answer this question.
Thanks
 
Ian Heff
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sony,
I am unsure here but my best guess is that maybe certain escape characters are allowed and others are not. Hopefully someone else will chime in a give us both clarification.
Ian
 
Ranch Hand
Posts: 315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am confused about the backward and forward slash
"/" "//" "\" "\\"
and also the usage for single and double quote.
Any one can help me? Thanks!
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I will be pretty upset if I see this kind of question on the real exam.
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
These characters are turned into their actual representation during the compile. So the new line character actually inserts a new line into your code which causes the compile error.
Quote usage: single quotes are used for chars and double quotes for Strings.
Slash usage:
/ = just another character with no special meaning
\ = escape character used to represent special stuff like the new line character, the tab character, etc. \n = new line; \t = tab; etc.
\\ is used because a single \ means "the next character is an escape character" so when you need a \ in your string, you have to specify two of them.
 
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Check out the following thread on unicodes:
Unicode
Clement
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
n a Java program, if the Unicode escape sequence \u000A, for example, occurs within a single-line comment, it is interpreted as a line terminator (Unicode character 000A is line feed) and therefore the next character is not part of the comment.
Similarly, if the Unicode escape sequence \u000A occurs within a string literal in a Java program, it is likewise interpreted as a line terminator, which is not allowed within a string literal.
One must write \n instead of \u000A to cause a line feed to be part of the string value of a string literal.
Because Unicode escapes are processed very early, it is not correct to write '\u000a' for a character literal whose value is linefeed (LF); the Unicode escape \u000a is transformed into an actual linefeed and the linefeed becomes a LineTerminator, and so the character literal is not valid. Instead, one should use the escape sequence '\n. Similarly, it is not correct to write '\u000d' for a character literal whose value is carriage return (CR). Instead, use '\r'.
 
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I found an excellent explanation of the background of this question at
http://www.absolutejava.com/java-tidbits/ - Unicode sequences that can kill you.
Actually I don't think that Sun has this kind of question in their exam; but it certainly doesn't harm to know about this problem. (By the way, I've passed the SCJP exam.)
 
Every plan is a little cooler if you have a blimp. And a tiny ad.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic