This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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 Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "printing of char value" Watch "printing of char value" New topic
Author

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
System.out.println(b);
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!!

Thanks
Rahul
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
1.
char ch = 'A';
System.out.println(ch); // prints A
this is the literal representation of A.
2.
char ch = 65;
System.out.println(ch); // prints A
here the ASCII representation of A is used
hope this answers the question
Samith.P.Nambiar
---------------------------------------------
the harder u try the luckier u get
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: printing of char value
 
Similar Threads
unicode character
Unicode Value????
Char assignment
'\u000d' and '\u000a' are not valid ?
Compiler Error in Commented Line. Why ?