This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Interestingly, it is related to compile time constants... See this excerpt from the JLS.
Section 5.2 of the JLS wrote:In addition, if the expression is a constant expression (§15.28) of type byte, short, char or int :
A narrowing primitive conversion may be used if the type of the variable is byte, short, or char, and the value of the constant expression is representable in the type of the variable.
A narrowing primitive conversion followed by a boxing conversion may be used if the type of the variable is :
--> Byte and the value of the constant expression is representable in the type byte.
--> Short and the value of the constant expression is representable in the type short.
--> Character and the value of the constant expression is representable in the type char.
Basically, for compile time constants, the compiler will allow certain wrapper types, if it can confirm that the constant is within range. And from the excerpt, you can see that Long is not on that list of supported wrapper types.
Hari MohanSingh wrote:
how does this apply?
Long myLONG = myint;
I am trying to widen not narrow.
It's the section that applies -- the section that talks about boxing of compile time constants. If you remove the final modifier, or anything else that makes it no longer a compile time constant, then that section no longer applies, and the code will no longer compile.
And yes, I agree that you are not narrowing... but that wasn't the point. You can feel free to ignore the part about narrowing; you are only using the portion of the JLS that talks about the boxing of compile time constants (Maybe I wasn't clear enough about this, in my previous post, sorry).
Anyway, regarding whole number constants, they only come in two flavors -- integer and long. So, your variable, myShort, is actually an integer constant. And your code that works, is simply boxing.