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?
Current Status:<br /> <br />SCJP 1.4<br />SCJD (in progress)
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
and your means
So though the number is big no error ever occurs
Hope this explains
Wish you all the best for the exam
Joined: Jun 25, 2004
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?
Joined: Aug 11, 2004
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
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 ]