• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unicode representation of a character

 
Sunita Vontel
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please see this code
class a
{
public static void main(String args[])
{
char a = '\u000A';
}
}
I am getting an error saying invalid character constant.Why is '\u000A' invalid.It is a hexadecimal literal right.
and when I comment out that line
class a
{
public static void main(String args[])
{
// char a = '\u000A';
}
}
the compiler still gives an error why??
a.java:5: Invalid character constant.
// char a = '\u000A';
^
a.java:5: Invalid expression statement.
// char a = '\u000A';
^
a.java:5: ';' expected.
// char a = '\u000A';
^
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13055
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hee heee that is a tricky one indeed! Here is why:
When the compiler reads Unicode characters it translates them on the fly - \u000A translates to a carriage return -BUT
this breaks up the line of code and you get:
char a =
;
Obviously an invalid statement. Thats why you have to use the escaped forms \r\n to represent carriage return and line feed
in Strings and character constants. They only get translated after the line is read and parsed.
Bill
 
eskay kumar
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sunitha,
Can't figure out why u get that. but when i try the code with the decimal equivalent of '\u000A' i.e. char c=10 i get a blank new line in the output - implying 10 ought to represent the special escape sequence '\n' for new line.
 
Sunita Vontel
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks william
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does that mean we can't use
char c ='\u000A';
at any time, instead we should use
char c = '\n' for that?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic