• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

char literals

 
MInu
Ranch Hand
Posts: 517
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
char ch='\u000A';
Whys this code cause a compile time error?
Can anyone explain me abt char literals...
 
Jeff Bosch
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Vad Fogel
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mani Ram
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jeff Bosch
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Depends on how many of the following I've had:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic