• Post Reply Bookmark Topic Watch Topic
  • New Topic

Char literal assignment Question  RSS feed

 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone! Hope you're havin' a good day so far,
Me not so hot, I've a doubt about the literal values that can be assigned to a char primitive.

As far as I know, the proper or most regular literal assignment to a char is a single character wrapped by single quotes like '@'.
Or assigning an Integer value, like 64.

What I'd like to know is what exactly happens behind scenes when I assign using a numerical value to a char, because If I try this:



It prints out the @ character. I need to know what Conversions happen internally for this to happen.?

Does it convert 64 to a Binary number first? then to ASCII?
Or does it convert 64 to Binary and then Unicode? I'd like to know.(cuz I dunno)

And well, I know the number can't be higher than 65535, because a char is a 16-bit Integer. But when I put a cast, a higher number is certainly allowed.
But Why?
and assigning 70000 for example to a char results in the character ?
Why is that?
In summary, I would love If someone explained to me all the details of the conversions that happen back-stage in order for a number assigned to a char to print out as a character.(And Why larger than 65535 is allowed?)

That's it... I hope this question isn't too basic...excuse my ignorance
Sincerely,
Jose
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't really make any sense to talk about converting from an integer to binary to Unicode; there's really no difference. A char is a small unsigned (non-negative) integer type with values between 0 and 65535 .

Here is everything you ever wanted to know about Unicode characters. The section entitled "Basic Latin" corresponds roughly to the ASCII character set. If you look up character number 64 in that Basic Latin chart (hexadecimal 40, cause 64 is 4 times 16), you'll see that it's the '@' character. The char variable holds the number 64, and when System.out.println() sends character 64 to the screen, it gets interpreted as a '@'.

Now, as far as larger numbers go: if you say

char c = (char) 70000;

the cast just discards the higher order bits. Basically you get the integer modulo 65536; here that's 4464. That number is way high up out of the Latin alphabets; it is in fact the code named "ETHIOPIC SYLLABLE SHA". Unless your machine is set up with Ethiopic fonts and script support, it is going to just give up and display (depending on the OS) a square box, or a question mark, or some other "I don't know" symbol.

Does that answer all your questions?
 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:


the cast just discards the higher order bits. Basically you get the integer modulo 65536; here that's 4464.

Does that answer all your questions?


Hello Mr. Friedman-Hill, your reply has been really useful, and it almost answers all my questions.

Could you please explain to me once more how is the modulo 65536 obtained? and given your example; how it gets translated to 4464?

Thank you very much sir.
Sincerely,
Jose
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
basically, when you say "A modulo B", you want to know what the remainder is when you divide A by B.

17 divided by 5 is 3, with a remainder of 2. So, 17 modulo 5 is 2.

similarly, 70000 modulo 65536 is the remainder when you divide.

70000 divided by 65536 is 1, with a remainder of 4464.

hence, 70000 modulo 65536 = 4464
 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello there ! I'm back.

Oh, the part about the modulo is totally understood now. thank you.
Mr. Rosenberger, May I ask one final question? Where could I find a table of values to see what are the character values as Integers? (Just a simple one, Integer value and character it generally represents) Just in case someone knows.

Thank you.
Best Regards,
Jose
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://www.asciitable.com contains the first 255 characters (scroll down for the second group).
 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey there Mr. Prime.!
that's exactly what I needed. you're a diamond.
Thanks for all the help!

See you next time!

Jose
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!