aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt on char  assignment Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubt on char  assignment" Watch "doubt on char  assignment" New topic
Author

doubt on char assignment

Kavita Ghia
Ranch Hand

Joined: Oct 10, 2000
Posts: 91
I was playing around with the following code and found the result very surprising.
char e12 = -12; //illegal assigning an int to a char. required char ==> OK
char e12 = (char) -12; //COMPILED
System.out.println("e12=" + e12);
OUTPUT:
?
Out of inqusitiveness i changed the values of char to -1, -2, -12, -13. In all the cases the o/p produced was ?.
Please explain why the same o/p for different values of char.
Mika Leino
Ranch Hand

Joined: Jan 07, 2002
Posts: 56
Could it be that none of those characters are in the character set your console uses? Then the characters would be replaced with question marks as the real character is unknown.
After all, -12 is 11111111 11111111 11111111 11110100 in binary. When the it's casted to char it becomes 11111111 11110100 which is quite high on the unicode character set. I recall reading that the first 128 characters are normal ASCII characters.
Someone correct me if I'm totally in the woods here.
[ September 25, 2003: Message edited by: Mika Leino ]

Mika Leino<br />SCJP, SCWCD
MSanjeevMehra
Greenhorn

Joined: Sep 18, 2003
Posts: 17
you are correct.
I recall reading that range of char is from 0 to 65535.
Someone correct me if I'm totally in the woods here.
[ September 25, 2003: Message edited by: MSanjeevMehra ]

Wish U Happiness,<br />Sanjeev.
Hanna Habashy
Ranch Hand

Joined: Aug 20, 2003
Posts: 532
hi:
you guys right about that. The regular ascii code range from 0 to 127, and the extended ascii code range is from 128 to 255.
When you assign a number to a char variable not in the range between 0 and 127 (inclusive), the char variable will be assigne the character '?'.
I think, but I am not sure, that you need a special version of operating system along with special version of JVM to be able to print and code in extended ascii code characters.
note: any negative number will be represented as '?'


SCJD 1.4<br />SCJP 1.4<br />-----------------------------------<br />"With regard to excellence, it is not enough to know, but we must try to have and use it.<br />" Aristotle
Kavita Ghia
Ranch Hand

Joined: Oct 10, 2000
Posts: 91
Dear All,
Thanks for the spontaneous reply.
Kavita
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
You can get more information about from:
unicode.org
Mostafa Radwan
Greenhorn

Joined: Sep 09, 2003
Posts: 25
Hi guys !
You all are right about this problem but the point hear is that if you did cast to that char ('?') to say "int" or "long you will get the same negative (of course or postive)value that char stores , so if it treates all negative numbers (as characters) and print them as '?' so why when it was casted to signed numerical type we get the exact value, this is the Biggest surprise here , and hope to find a n explnation to it .
Thanks .


- Mostafa Radwan -
M.Sc | SCJP 1.4
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Hi guys !
You all are right about this problem but the point hear is that if you did cast to that char ('?') to say "int" or "long you will get the same negative (of course or postive)value that char stores , so if it treates all negative numbers (as characters) and print them as '?' so why when it was casted to signed numerical type we get the exact value, this is the Biggest surprise here , and hope to find a n explnation to it .
Thanks .

We do not same exact value:
Mostafa Radwan
Greenhorn

Joined: Sep 09, 2003
Posts: 25
Hi Guys!
Yes you are right Barakat about int and long but i forgot to also mention byte , try this code :


so why it is applicable here on 'byte' and don't on 'int' ?
Hanna Habashy
Ranch Hand

Joined: Aug 20, 2003
Posts: 532
hi:
Here is my explenation for the mysterious behavior.
char is a 16 bit UNSIGNED literal. Any number assigned to a char literal greater than 127, will be presented as '?'. Now comes the trick of char leterals. If you assigne a negative number to a char literal, it will be dealt with as a positive number, becuase again, char literal is UNSIGNED LITERAL.e.x.
1) char c = -7; // in binary: 11111111 11111001 = (unsigend) 65527 > 127 will be presented as '?'
if you cast this char to a byte; byte is a signed 8 bit literal.
2) byte b = (byte)c; // in binary: 11111001 = -7
Look at what happen when you cast the same char to an int.
because int literal is a signed 32 bit literal
3) int i = (int)c; // in binarry: 00000000 00000000 11111111 11111001 = 65527.
note that, in line (1) we evaluated the binary number as unsigned.
Correct me if I am wrong
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doubt on char assignment