• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Assigning a number literal to a char

 
Liang Anmian
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

I'm currently studying for the Sun Certified Programmer examination. This is my first time. Hope I can pass in one attempt! Wish me luck!

Anyway, I have a tiny problem understanding a concept. You see, on page 15 of the study guide, it states that since char is nothing but a 16-bit unsigned integer, it is perfectly legal to assign a number between 0 to 65535 to a char, and Java will interpret this as the ASCII/Unicode code.

However, supposed I have this:



Why is this legal during compilation and runtime? Even if you perform an explicit cast, the ASCII/Unicode code is still too large (limit 65535). What does the JVM do in the background?
 
Oneal Shaha
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Liang,

if you assign a value to the variable which is exceeding the capacity of the variable then the JVM starts counting from again the lowest limit.

So if you assign to any value greater that the 65536 to chat variable it will start couning from zero
So the statement

actually means


and your
means

So though the number is big no error ever occurs

Hope this explains

Wish you all the best for the exam
 
Liang Anmian
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean that if this number exceeds 65535, then the JVM will keep subtracting 65536 (because 0 is counted) until it obtains a number 65535 or below? Then the JVM uses this number as the ASCII/Unicode code?

Did I understand you correctly?
 
Oneal Shaha
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes!
You got what I wanted to say!

Only thing is that i really don't know this substraction is done by JVM.
I think it is somewhere in the constructor of wrapper class or in some other class

But at the end what happens is what I said
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Liang Anmian:
Do you mean that if this number exceeds 65535, then the JVM will keep subtracting 65536 (because 0 is counted) until it obtains a number 65535 or below? Then the JVM uses this number as the ASCII/Unicode code?
Yes, that's the effect, although what it really does is simply throw away any extra bits - i.e. bit 16 and higher, if we count bits from 0. Note that 2 to the power 16 is 65536.

- Peter
[ September 07, 2004: Message edited by: Peter den Haan ]
 
Liang Anmian
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok thank you very much. That was helpful.
 
Tom Tolman
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What it does is convert the number into a bit representation. Then it gets rid of all the bytes which are not utilized. 70000 is an integer.


If we move to hexadecimal,

integer 65535 = hexadecimal ffff (fits in 2 bytes)
integer 65536 = hexadecimal 10000 (fits in 3 bytes)
integer 70000 = hexadecimal 11170 (fits in 3 bytes)

Truncate off the high byte of 11170 = 1170 in hexadecimal because a char only stores 2 bytes.
This equals 4464 as a 2 byte char.
[ September 07, 2004: Message edited by: Tom Tolman ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic