Meaningless Drivel is fun!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes printing of char value 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 "printing of char value" Watch "printing of char value" New topic

printing of char value

Rahul Ramachandran
Ranch Hand

Joined: Feb 27, 2001
Posts: 48
Here's an easy question:
char a = 10;
System.out.println(a); //printouts a blank space
char b = '\u000a'; //gives a compilation error
The unicode value \u000a evaluates to decimal 10. Why is this error occurring?
However, assigning \u0009, \u0008 or \u0007(space with a ding) does not cause an error!!

Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
When in doubt look in the JLS:
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'.
Moderator of the Programmer Certification Forums

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

Joined: Nov 01, 2000
Posts: 37
is it not necessary to include ''for char value. for eg. char c='1'; throw some light.
Samith Nambiar
Ranch Hand

Joined: Mar 14, 2001
Posts: 147
hi bala
char ch = 'A';
System.out.println(ch); // prints A
this is the literal representation of A.
char ch = 65;
System.out.println(ch); // prints A
here the ASCII representation of A is used
hope this answers the question
the harder u try the luckier u get
I agree. Here's the link:
subject: printing of char value
It's not a secret anymore!