Java Reference
In-Depth Information
14.17. The return Statement
A return statement returns control to the invoker of a method (§ 8.4 , § 15.12 ) or constructor
8.8 , § 15.9 ) .
ReturnStatement:
return Expression opt ;
A return statement is contained in the innermost constructor, method, or initializer whose
body encloses the return statement.
It is a compile-time error if a return statement is contained in an instance initializer or a
static initializer (§ 8.6 , § 8.7 ) .
A return statement with no Expression must be contained in a method that is declared, using
the keyword void , not to return any value (§ 8.4 ) , or in a constructor (§ 8.8 ), or a compile-
time error occurs.
A return statement with no Expression attempts to transfer control to the invoker of the
method or constructor that contains it. To be precise, a return statement with no Expression
always completes abruptly, the reason being a return with no value.
A return statement with an Expression must be contained in a method declaration that is de-
clared 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.
A return statement with an Expression attempts to transfer control to the invoker of the
method that contains it; the value of the Expression becomes the value of the method in-
vocation. More precisely, execution of such a return statement first evaluates the Expres-
sion . If the evaluation of the Expression completes abruptly for some reason, then the re-
turn statement completes abruptly for that reason. If evaluation of the Expression completes
normally, producing a value V , then the return statement completes abruptly, the reason be-
ing a return with value V .
If the expression is of type float and is not FP-strict (§ 15.4 ), then the value may be an ele-
ment of either the float value set or the float-extended-exponent value set (§ 4.2.3 ) . If the
expression is of type double and is not FP-strict, then the value may be an element of either
the double value set or the double-extended-exponent value set.
Search WWH ::




Custom Search