This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt on char assignment

 
Kavita Ghia
Ranch Hand
Posts: 91
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 56
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
MSanjeevMehra
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Hanna Habashy
Ranch Hand
Posts: 532
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 '?'
 
Kavita Ghia
Ranch Hand
Posts: 91
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear All,
Thanks for the spontaneous reply.
Kavita
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can get more information about from:
unicode.org
 
Mostafa Radwan
Greenhorn
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 .
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 532
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic