• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

printing of char value

 
Rahul Ramachandran
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Balaguru Janarthanan
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is it not necessary to include ''for char value. for eg. char c='1'; throw some light.
 
Samith Nambiar
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic