Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

char literals java  RSS feed

 
fahmida tasnim prema
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
char d=(char)-255;
when i print char d , it prints !.... but in ascii table '!' it represents 33 .
first question:
can any one show me the conversion ?
second question:
my another question is why i want to use a negative integer value and cast it to char where clearly only positive integer value represents character  ??
third question:
char c=(char)7000;
above statement is legal. but i want to know why ? we know the range is between 0 to 65535. so why we even using 7000? and also it prints nothing in netbeans




 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To understand this, you'll have to know a little bit more about what the char type is exactly and how it represents characters.

The char type is a 16-bit unsigned integer, which represents an UTF-16 encoded Unicode character.

To understand why -255 translates to the character "!", you first have to look at the bits of the integer -255. Integers are stored using two's complement format, so the bits of an int with the value -255 are: 11111111111111111111111100000001

Now you cast this to a char, which means the upper 16 bits are dropped and you end up with a char containing the bits: 1111111100000001

In hexadecimal, this is FF01. And the Unicode character with code FF01 is the fullwidth exclamation mark.

I don't know why anyone would want to use a negative integer in practice. Is this just a puzzle question that you read somewhere? In general, when you write code, you should write it so that it is as clear as possible what it means. Doing tricks like this isn't a good thing in real code.

Your second question: You already know that a char can contain values between 0 and 65536, so why are you asking why the value 7000 is legal? It's in the range, so there's nothing special about it.

Which character: 7000 decimal = 1B58 (hexadecimal) which is the Balinese digit eight.

Why you are not seeing that character when you run it in Netbeans: Because Netbeans is using a font that does not contain that character. Most fonts do not contain the full range of Unicode characters, and when you use a character that is not in the font you usually get a space or a "?" or some other replacement character.






 
fahmida tasnim prema
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry my mistake .i wanted to write 70000 instead of 7000. i found this on the  java study guide  Kathy Sierra and Bert Bates page 172
thank you
 
fahmida tasnim prema
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i understood you explanation . but can you tell me how can i get ascii value 33 ?
http://www.asciitable.com/
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fahmida tasnim prema wrote:i understood you explanation . but can you tell me how can i get ascii value 33 ?
http://www.asciitable.com/


The lower values of Unicode are compatible with ASCII. So, the value of 33, when represented as a char, has a Unicode value of 33... and the character when used/printed is the same as ASCII.

Henry
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fahmida tasnim prema wrote:sorry my mistake .i wanted to write 70000 instead of 7000.

Just like with -255 or any other value, you take the lower 16 bits of the value, and that is put into the char.

70000 (dec) = 0000 0000 0000 0001 0001 0001 0111 0000 (bin) => lower 16 bits = 0001 0001 0111 0000 (bin) = 1170 (hex) = Cyrillic Capital Letter Ghe with Stroke
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!