• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question about char  RSS feed

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Jessica Sant:

char a = '\0043'; YES -- this is 67 in decimal


I maybe wrong, but did you mean to say '\u0043' instead of '\0043'?
Thanks
Harwinder
 
Suneela Joshi
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sorry, I typed it wrong in the post, I did try \u000A in my
example. Thanks for the quick response.
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Vad Fogel
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 '�'.
 
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The octal escape representation of a char has the following format

Thanks.
[ November 11, 2003: Message edited by: Cathy Song ]
 
Evil is afoot. But this tiny ad is just an ad:
Thread Boost - a very different sort of advertising
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!