Melissa Nikolic wrote:Hi again,
I wanted to confirm a particular conversion example and its meaning.
If I initialize the following:
short h = 40;
My Java book says that this is ok because int is converted to a short.
while...
h = h + 2; is not okay because "cannot assign an int to short"
Is this an error because the first example is explicitly assigned to a short while the second example is not explicitly assigned and therefore cannot be promoted implicitly?
Campbell Ritchie wrote:I have already told you the Java Language Specification is difficult to understand, but that is where you will find the details.
Jeff Verdegan wrote:
Melissa Nikolic wrote:Hi again,
I wanted to confirm a particular conversion example and its meaning.
If I initialize the following:
short h = 40;
My Java book says that this is ok because int is converted to a short.
while...
h = h + 2; is not okay because "cannot assign an int to short"
Is this an error because the first example is explicitly assigned to a short while the second example is not explicitly assigned and therefore cannot be promoted implicitly?
Case 1: 40 is an int. Since it is a compile-time constant that fits into a short, a narrowing conversion is automatically applied and the value (short)40 is stored in h. No promotion occurs.
Case 2: h + 2 is short + int, so the value of h on the RHS is promoted to an int, and the result of h + 2 is an int. Since h + 2 is not a compile-time constant, no automatic narrowing is done, and it's an error because we're trying to stick an int into a container meant to hold only a short.
Melissa Nikolic wrote:
Thank you for your clear explanation. Frankly, telling a newbie to go to the Java documentation is like telling an introductory biology student to go find the answer to a question in a peer reviewed journal. It seems a bit unhelpful. But since I am used to peer reviewed journals I should be able to muck through the documentation.
Regards,
M
Jeff Verdegan wrote:
Melissa Nikolic wrote:
Thank you for your clear explanation. Frankly, telling a newbie to go to the Java documentation is like telling an introductory biology student to go find the answer to a question in a peer reviewed journal. It seems a bit unhelpful. But since I am used to peer reviewed journals I should be able to muck through the documentation.
Regards,
M
Well, it's just two different approaches. There's a continuum of how much direct help can be offered vs. just pointing someone in the right direction and letting them try to figure it out on their own, and, other than not handing over full code solutions, there are as many different opinions on what kind of help should be given as there are people giving help. For beginners, I usually just provide the JLS as additional information, since a lot of it can be difficult reading, even for those with more experience. I guess Campbell felt it was better for you to read the relevant section of the JLS first, and then ask more specific questions if they arise from there. Either way, we're all just trying to help you learn.![]()
Melissa Nikolic wrote:I did go to the documentation suggested and I have another question. In "Narrowing Primitive Conversions" http://java.sun.com/docs/books/jls/second_edition/html/conversions.doc.html#25363 the documentation states the following:
The following 23 specific conversions on primitive types are called the narrowing primitive conversions:
byte to char
short to byte or char
char to byte or short
int to byte, short, or char
long to byte, short, char, or int
float to byte, short, char, int, or long
double to byte, short, char, int, long, or float
Everything makes sense to me but the narrowing of a byte to a char. Why would a narrowing conversion take place from a byte (8 bits) to a char (16 bits)? And why would you narrow the conversion from a byte to a char but not from a byte to a short (which has the same bit width as the char)?
Consider Paul's rocket mass heater. |