aspose file tools
The moose likes Beginning Java and the fly likes Char literal assignment Question Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Char literal assignment Question" Watch "Char literal assignment Question" New topic
Author

Char literal assignment Question

Jose Campana
Ranch Hand

Joined: May 28, 2007
Posts: 339
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
Marshal

Joined: Jul 08, 2003
Posts: 24081
    
  15

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?


[Jess in Action][AskingGoodQuestions]
Jose Campana
Ranch Hand

Joined: May 28, 2007
Posts: 339
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

Joined: Oct 02, 2003
Posts: 10043
    
    6

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


Never ascribe to malice that which can be adequately explained by stupidity.
Jose Campana
Ranch Hand

Joined: May 28, 2007
Posts: 339
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

Joined: Oct 27, 2005
Posts: 19232

http://www.asciitable.com contains the first 255 characters (scroll down for the second group).


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Jose Campana
Ranch Hand

Joined: May 28, 2007
Posts: 339
Hey there Mr. Prime.!
that's exactly what I needed. you're a diamond.
Thanks for all the help!

See you next time!

Jose
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Char literal assignment Question
 
Similar Threads
doubt
Sun Cirtification
Doubt for char in K&B
char representation question -- was please guide
chars