wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Assigning a number literal to a char Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Assigning a number literal to a char" Watch "Assigning a number literal to a char" New topic
Author

Assigning a number literal to a char

Liang Anmian
Ranch Hand

Joined: Jun 25, 2004
Posts: 119
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?


Current Status:<br /> <br />SCJP 1.4<br />SCJD (in progress)
Oneal Shaha
Ranch Hand

Joined: Aug 11, 2004
Posts: 98
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


-Oneal
Liang Anmian
Ranch Hand

Joined: Jun 25, 2004
Posts: 119
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

Joined: Aug 11, 2004
Posts: 98
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

Joined: Apr 20, 2000
Posts: 3252
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

Joined: Jun 25, 2004
Posts: 119
Ok thank you very much. That was helpful.
Tom Tolman
Ranch Hand

Joined: Sep 02, 2004
Posts: 83
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Assigning a number literal to a char
 
Similar Threads
convert character to integer
Assigning to char
how to avoid user to enter a numeric or alfanumeric input to JTextField
char range
chars