Short s = 10;
This is an Assignment Conversion
, and 10
is a constant expression. JLS said:
5.2. Assignment Conversion
Assignment conversion occurs when the value of an expression is assigned to a variable: the type of the expression must be converted to the type of the variable.
......
In addition, if the expression is a constant expression of type byte, short, char, or int:
- A narrowing primitive conversion followed by a boxing conversion may be used if the type of the variable is:
-
- Short and the value of the constant expression is representable in the type short.
takeShort(10);
This is a Method Invocation Conversion
. JLS said:
5.3. Method Invocation Conversion
Method invocation conversion is applied to each argument value in a method or constructor invocation : the type of the argument expression must be converted to the type of the corresponding parameter.
Method invocation contexts allow the use of one of the following:
- an identity conversion
- a widening primitive conversion
- a widening reference conversion
- a boxing conversion optionally followed by widening reference conversion
- an unboxing conversion optionally followed by a widening primitive conversion.
......
If the type of the expression cannot be converted to the type of the parameter by a conversion permitted in a method invocation context, then a compile-time error occurs.
Unlike Assignment Conversion, non of conversion listed above can converts int
to Short
, so a compile-time error occurs.
Unfortunately some has rejected kiruwka's edit before I can approve it, so I edit it myself
The example of Method invocation conversion:
// takeInteger(int) takeDouble(double) takeObject(Object) takeIntegerObject(Integer)
takeInteger(5); // an identity conversion
takeDouble(5); // a widening primitive conversion
takeObject(new Integer(5)); // a widening reference conversion
takeIntegerObject(5); // a boxing conversion
takeObject(5); // a boxing conversion followed by widening reference conversion
takeInteger(new Integer(5)); // an unboxing conversion
takeDouble(new Integer(5)); // an unboxing conversion followed by a widening primitive conversion.