aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Unicode representation of a character Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Unicode representation of a character" Watch "Unicode representation of a character" New topic
Author

Unicode representation of a character

Sunita Vontel
Ranch Hand

Joined: Aug 28, 2000
Posts: 72
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

Joined: Mar 22, 2000
Posts: 12769
    
    5
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

Joined: Jul 22, 2000
Posts: 71
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

Joined: Aug 28, 2000
Posts: 72
Thanks william
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Does that mean we can't use
char c ='\u000A';
at any time, instead we should use
char c = '\n' for that?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Unicode representation of a character