aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes char literals 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 "char literals" Watch "char literals" New topic
Author

char literals

MInu
Ranch Hand

Joined: Oct 09, 2003
Posts: 517
char ch='\u000A';
Whys this code cause a compile time error?
Can anyone explain me abt char literals...


God Gave Me Nothing I Wanted<br />He Gave Me Everything I Needed<br /> - Swami Vivekananda
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 805
Hi, Minu -
Welcome to JavaRanch.
The compiler happens because you're telling the compiler to end the line!
Here's what the compiler sees:

becomes

and that's a compiler error because the line is not terminated properly, nor is there a "right value" to assign to the variable ch. Why does it do that? Because Java source code files are considered Unicode by default.
[ October 10, 2003: Message edited by: Jeff Bosch ]

Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
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 in translation step 1 (�3.3) and the linefeed becomes a LineTerminator in step 2 (�3.4), and so the character literal is not valid in step 3. Instead, one should use the escape sequence '\n' (�3.10.6). Similarly, it is not correct to write '\u000d' for a character literal whose value is carriage return (CR). Instead, use '\r'.

3.10.4 Character Literals
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Originally posted by Jeff Bosch:
Because Java source code files are considered Unicode by default.
You could write your entire program in unicode if you liked. It would be hard to debug though.


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

Joined: Mar 11, 2002
Posts: 1140
Originally posted by Thomas Paul:
You could write your entire program in unicode if you liked. It would be hard to debug though.

Is the following program (UC.java) hard to debug?
\u0070\u0075\u0062\u006C\u0069\u0063\u0020\u0063\u006C\u0061\u0073\u0073\u0020\u0055\u0043\u0020\u007B\u000D\u0009\u0070\u0075\u0062\u006C\u0069\u0063\u0020\u0073\u0074\u0061\u0074\u0069\u0063\u0020\u0076\u006F\u0069\u0064\u0020\u006D\u0061\u0069\u006E\u0028\u0053\u0074\u0072\u0069\u006E\u0067\u0020\u0061\u0072\u0067\u0073\u005B\u005D\u0029\u0020\u007B\u000D\u0009\u0009\u0053\u0079\u0073\u0074\u0065\u006D\u002E\u006F\u0075\u0074\u002E\u0070\u0072\u0069\u006E\u0074\u006C\u006E\u0028\u0022\u0048\u 0065\u006C\u006C\u006F\u0022\u0029\u003B\u000D\u0009\u007D\u000D\u007D


Mani
Quaerendo Invenietis
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 805
Depends on how many of the following I've had:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: char literals