Hi All I tried this char a = 67; char b = 10; System.out.println(""+a+b); This compiles and runs fine giving output of C and a blank But if I convert the same numbers into unicode characters char a = '\0043'; char b = '\0010'; This does not compile. Can someone tell me why? Thank you.
gotta be careful with your HEX conversion: char a = 67; char b = 10; char a = '\0043'; YES -- this is 67 in decimal char b = '\0010'; NO -- this is NOT 10 in decimal!!! BUT -- more importantly -- unicode charcters are converted before the class is compiled (check out this question on the FAQ: char a = '\u000A'. Why is this invalid?) so -- basically, your code gets converted into the following: char a = a; char b = ; [ November 10, 2003: Message edited by: Jessica Sant ]
Originally posted by Suneela Joshi: I am sorry, I typed it wrong in the post, I did try \u000A in my example. Thanks for the quick response.
You may not be so wrong BTW . Look at this:
The following are examples of char literals: 'a' '%' '\t' '\\' '\'' '\u03a9' '\uFFFF' '\177' 'W' '�' 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'.
As we see, a character can also be represented by an octal sequence like '\177' or '\54'. Because they are resolved very early, these sequences must not violate the compiler rules. Look into Dan's exams for more on char representation. The real exam won't test you on that.
Hi i checked for char c='\177' and it compiles fine but when i checked for certain other values like '\777' or '\727' it does not compile. what is the reason?
posted 15 years ago
Originally posted by sonali rao: Hi i checked for char c='\177' and it compiles fine but when i checked for certain other values like '\777' or '\727' it does not compile. what is the reason?
The last octal sequence you can get away with is char '\377' , which equals 255 in decimal. I can give you my most educated guess: '\377' is not in Unicode notation, and decimal 255 prompts me to say that this is the upper limit of ASCII characters set. ASCII is a one byte notation (0-255), and the last valid character there is 255 or '�'.