This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Valid Declarations

 
Steve Ram
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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 answer...you can replace any char with unicode equivalent..but i tried it in SunOne IDE and the compilation fails.
Am i doing anything wrong?
Thanks
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
studentforever,
Welcome to Javaranch, a friendly place for Java greenhorns
You can find the answer to this question in the following discussion:
http://www.coderanch.com/t/240779/java-programmer-SCJP/certification/ch-valid
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!
 
Steve Ram
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Author
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Veena Pointi
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
Thanks
Veena
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is the compiler who translates Unicode escapes to Unicode characters.
See for JLS 3.2 more.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic