File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "interesting problem in char" Watch "interesting problem in char" New topic

interesting problem in char

sony ali

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

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

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

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

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

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
Check out the following thread on unicodes:

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

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 - 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.)

I agree. Here's the link:
subject: interesting problem in char
It's not a secret anymore!