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 Valid Declarations Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Valid Declarations" Watch "Valid Declarations" New topic

Valid Declarations

Steve Ram

Joined: Jun 16, 2003
Posts: 2
Got this question from Mughal/Rasmussen:-
Which of the following lines are valid declarations? Select all valid answers
a) char a = '\u0061';
b) char \u0061 = 'a';
c) ch\u0061r a = 'a';
The answer says everything is valid.
I can understand the logic behind the can replace any char with unicode equivalent..but i tried it in SunOne IDE and the compilation fails.
Am i doing anything wrong?
Valentin Crettaz
Gold Digger

Joined: Aug 26, 2001
Posts: 7610
Welcome to Javaranch, a friendly place for Java greenhorns
You can find the answer to this question in the following discussion:
Moreover, we ain't got many rules 'round these parts, but we do got one. Please change your displayed name to comply with the JavaRanch Naming Policy.
Thanks Pardner! Hope to see you 'round the Ranch!

[Blog] [Blogroll] [My Reviews] My Linked In
Steve Ram

Joined: Jun 16, 2003
Posts: 2
Thanks Valentin and sorry about the name.
I still am not sure how to compile it.. Is there anything i should be importing or setting to tell the compiler i am using unicode char?
Thanks in advance.
James Chegwidden
Ranch Hand

Joined: Oct 06, 2002
Posts: 201
I did a test program with the problem p. 33.
The answer is correct
Did you type in each line separately and compile it?
If you type in all lines together it will try to redefine the literal which you can not do.
Got It?
Also, did you notice earlier in text about how Unicode is declared so
char a = '\u0061' - p25. Character literal valid
char \u0061 = 'a' - yep valid rewrite:
char a = 'u0061' just replaced
value - correct
ch\u0061r a = 'a' - think \u0061 is an a right?
So char a = 'a' - just replace the values.
Understand the meaning of a literal and the assignment operator
[ June 17, 2003: Message edited by: James Chegwidden ]

Author and Instructor, my book
Veena Pointi
Ranch Hand

Joined: Jun 20, 2002
Posts: 442
I tried ,compilation was fine.But I didn't understand the logic fully.does JVM replaces unicode value with the equivalent unicode character during runtime?

"Continuous effort - not strength or intelligence - is the key to unlocking our potential."
*Winston Churchill
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
It is the compiler who translates Unicode escapes to Unicode characters.
See for JLS 3.2 more.

SCJP2. Please Indent your code using UBB Code
I agree. Here's the link:
subject: Valid Declarations
It's not a secret anymore!