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

Invalid Chararcter constant

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Can anyone explain why I get an Invalid character constant error with the following line of code
//char a = '\u000A';
+ Why does the above not compile but the following does
/*char a = '\u000A';*/
Any help would be truly appreciated
Tony
 
Junilu Lacar
Bartender
Posts: 7572
52
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony,
Please re-register with a UserName that conforms to the JavaRanch Naming Policy. Thanks for your cooperation.
Unicode literals are translated very early on in the compilation process. '\u000A' happens to be the line feed and the result is that the compiler tries to compile
<pre>
// char a = '
';
not good because // is only for one line

/* char a = '
'; */
OK because /* */ spans lines
</pre>

------------------
Junilu Lacar
Sun Certified Programmer for the Java� 2 Platform
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you look at the Unicode Charts yout will see that \u000A is unicode for Line Feed.
Since the .java file is parsed before compiling, and all of the unicode literals are converted first, the input line is actually split into 2 lines by the LF.
Then as Junilu said, the // comment syntax does not include both of those lines.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic