Asignment conversion (JLS 5.2) is applied when: assigning an expression to a variable. comparing the constant after "case" with the expression within "switch(..here..)" (No reference conversions allowed in this case). returning an expression from a method to the type returned by the method. invoking a method: type of the parameters to the type of the arguments. (No implicit primitive narrowing conversion) runtime checks for casting. anymore? maybe the type of the exception thrown to the type of the exception caugth.
Actually I looked at the replies to this question yesterday and still did not see a good answer. If this comes up on the test what is the correct answer? It looks like according to the JLS you can do this (which I always though you could if the return value is a constant that will fit), so JQ+ is wrong here, correct?
Joined: Jan 07, 2002
Definitive Answer: (always comes from JLS): s14.16 The return Statement.
A return statement with an Expression must be contained in a method declaration that is declared to return a value. (s8.4) or a compile-time error occurs. The Expression must denote a variable or value of some type T, or a compile-time error occurs. The type T must be assignable (s5.2) to the declared result type the method, or a compile-time error occurs. s5.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. Assignment contexts allow the user of an identity conversion, a widening primitive conversion, or a widening reference conversion. In addition, a narrowing primitive conversion may be used if all of the following conditions are satisfied: * The expression is a constant expression of type byte, short, char, or int * The type of the variable is byte, short, or char * The value of the expression (which is known at compile time, because it is a constant expression) is representable in the type of the variable
So the JLS blesses this code and says it should compile just fine. Hope that's definitive enough for you!!