| Author |
Char with negative showing ?
|
Srinivas Redd
Greenhorn
Joined: Mar 23, 2006
Posts: 17
|
|
The following code after running showing the output as b=? Can anybody tell me why is this happening ???/ import java.lang.*; class FirstClass { char b= (char)-10; void print() { System.out.println("b "+b); } } class Run { public static void main(String argsv[]) { FirstClass FC = new FirstClass(); FC.print(); } }
|
Srinvasa Reddy Thamma,<br />Chennai,India.
|
 |
Keith Lynn
Ranch Hand
Joined: Feb 07, 2005
Posts: 2341
|
|
This is the bit represenation of -10. When it's converted to a char, you get the last two bytes. I believe the reason it's printing ? is that the last two bytes represent a character your machine can't display.
|
 |
Srinivas Redd
Greenhorn
Joined: Mar 23, 2006
Posts: 17
|
|
Hi , Thanks for your answer. I agree that the char takes the last two bytes from the 2's compliment value of -10 . It could be some big value like 65535 in decimals9base 10).... Here I guess , it would do some mod operation on 65535 and then changes to some value less than 256 ,which can be printable. Am I correct. Thanks in advance. Srinivas Reddy
|
 |
Keith Lynn
Ranch Hand
Joined: Feb 07, 2005
Posts: 2341
|
|
|
No I don't think it mods by 256. Large UNICODE values are probably used for characters of other languages or aren't used now.
|
 |
Srinivas Redd
Greenhorn
Joined: Mar 23, 2006
Posts: 17
|
|
Hi Lynn, Yes I agree ..... Thanks for your information. Srinivas Reddy
|
 |
 |
|
|
subject: Char with negative showing ?
|
|
|