| Java FAQ | ||
| JSP FAQ | ||
| Servlet FAQ | ||
XyzWs Java FAQ:
Does the implicit narrowing conversion work with the method return statement?
Printer-friendly version |
Mail this to a friend
|
Advertisement
|
Does the implicit narrowing conversion work with the method return statement?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
"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. |