Just trying to understand auto-boxing, which I do apart from one thing:
Short s = 250;
Long l = 250;
The assignment to Long l fails. This, I expect, is because you cannot widen then box (i.e. it tries to widen the int value 250 to a long and then box it which it cannot do).
However, the assignment to Short s works. What is going on to make this fine? My assumption was it is still doing boxing and some kind of conversion. But if it’s a case of it knowing 250 fits into a short, why does it not know that 250 will fit into a long?
Normally, you cannot apply multiple (implicit) conversions in assignment (JLS §5.2 Assignment Conversion):
Long l=250;requires two conversions (widening primitive conversion followed by boxing conversion), that’s why it doesn’t compile.Long l=250l;compiles because it requires a single boxing conversion.But narrowing conversion of a constant expression is a special case, that’s why
Short s=250;compiles: