Syntax
>>-return-+------------+-;->< '-Expression-'
Description
A return statement returns control to the invoker of a method or constructor.
To return no value, a return statement with no expression must be contained
in the body of a method that is declared, using the keyword void, or it must
be contained in the body of a constructor. A return statement with no
expression attempts to transfer control to the invoker of the method or constructor that
contains it.
A compilation error occurs if the following conditions are not met:
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 invocation. More precisely, execution of such a return statement
first evaluates the expression. If evaluation of the expression completes normally,
producing a value V, then the return statement completes normally.
The preceding descriptions say "attempts to transfer control" rather than just
"transfers control" because if there are any try statements within
the method or constructor whose try blocks contain the return
statement, then any finally clauses of those try statements will
be executed, in order, innermost to outermost, before control is transferred to the
invoker of the method or constructor. Abrupt completion of a finally clause
can disrupt the transfer of control initiated by a return statement.
If a return statement appears within a static initializer, a compilation error occurs.
Example
The following example is a simple use of the return statement.
public boolean stringsAreEqual(String s1, String s2) { return s1.equals(s2); }
If a method has a void return type, the return statement doesn't have any Expression.
public void doSomethingInteresting() { ... return; }
Source: The Java Language Specification. Copyright (C) 1996 Sun Microsystems, Inc.