Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes Char with negative showing ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Char with negative showing ?" Watch "Char with negative showing ?" New topic
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: 2367
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: 2367
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Char with negative showing ?