aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes interesting problem in char Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "interesting problem in char" Watch "interesting problem in char" New topic
Author

interesting problem in char

sony ali
Greenhorn

Joined: Feb 24, 2001
Posts: 9
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?



Ian Heff
Greenhorn

Joined: Mar 15, 2001
Posts: 12
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

Joined: Feb 24, 2001
Posts: 9
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
sajida kal
Ranch Hand

Joined: Mar 22, 2001
Posts: 89
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

Joined: Mar 15, 2001
Posts: 12
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
Jackie Wang
Ranch Hand

Joined: Apr 18, 2002
Posts: 315
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!
Wei Du
Greenhorn

Joined: May 03, 2002
Posts: 16
I will be pretty upset if I see this kind of question on the real exam.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
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.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Steven Wong
Ranch Hand

Joined: Mar 07, 2002
Posts: 295
Hi,
Check out the following thread on unicodes:
Unicode
Clement


best regards,<br />Steven<br />SCJP, SCEA
Ana P
Greenhorn

Joined: May 02, 2002
Posts: 10
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'.
Mag Hoehme
Ranch Hand

Joined: Apr 07, 2002
Posts: 194
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.)


Mag
 
Consider Paul's rocket mass heater.
 
subject: interesting problem in char
 
Similar Threads
Question from jqplus for equals()
byte to char casting
help me
char literal
char literal