--
Yes, the implicit narrowing conversion work with the method return statement. For example, class Program { byte method_1() { return 126; } byte method_2() { final int m = 127; return m; } byte method_3() { final int m=128; return m; //compile time error because m is not in the range value (-128 to 127) of byte } }
14.17 The return Statement in JLS 3.0:
A return
statement with an Expression must be contained in a method declaration that is declared to return a value (8.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 (5.2) to the declared result type of the method, or a compile-time error occurs.
"The type T must be assignable (5.2) to the declared result type of the method, or a compile-time error occurs" indicates that the implicit narrowing of integer constants is allowed in the method return context.
Comments
Post a Comment