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: 11862
posted
0
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
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
posted
0
Thanks william
Anonymous
Ranch Hand
Joined: Nov 22, 2008
Posts: 18944
posted
0
Does that mean we can't use char c ='\u000A'; at any time, instead we should use char c = '\n' for that?