Sridhar,
The reason you can do
is because of a special exception for Assignment Conversions only, stated in the
Java Language Specification:
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.
The reason you can do
is that the int can be widened to a long, and thus is acceptable for the constructor signature Long(long value). You can't do
for the opposite reason; you can't automatically narrow the integer. The automatic conversion listed above is only for assignment (Short s1 = 500 ;) .
As an aside, when using the primitive wrapper classes, it's generally preferable to use (for example) Long.valueOf(long); this will let the compiler take advantage of cached values when possible, thus minimizing the creation of new objects.
[ May 27, 2008: Message edited by: Stevi Deter ]