This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Char with negative showing ?

 
Srinivas Redd
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
}
}
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Lynn,

Yes I agree ..... Thanks for your information.

Srinivas Reddy
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic