return statement

Purpose:

To return a value from a method, or exit a method prematurely.

Mechanics:

return <expression>;

Example:

return 1;

Usage:

  • <expression> is a variable, function, or parameter -- or any combination of them, with operators and parentheses -- that evaluates to the same type as the return type for the method in which this statement appears. Upon encountering this statement, Java immediately halts further execution of the method and returns the value to which <expression> evaluates. All subsequent statements in the method (if any) are ignored.
  • If <expression> has multiple parts, as in a chain of arithmetic operations, then it is evaluated according to the heirarchy of operations.

Restrictions:

If the return type of the method in which this statement appears is void, or if it appears in a constructor, then <expression> must be omitted. Otherwise, it must evaluate to the same type as the return type.
Back to the Table of Contents
email suggestions to: cs015tas@cs.brown.edu