Java Reference
In-Depth Information
Returning from a Method
To return a value from a method when its execution is complete you use a return statement,
for example:
return return _ value; // Return a value from a method
After executing the return statement, the program continues from the point where the method was
called. The value, return _ value , that is returned by the method can be any expression that produces
a value of the type specified for the return value in the declaration of the method. Methods that return a
value - that is methods declared with a return type other than void - must always finish by executing a
return statement that returns a value of the appropriate type. Note, though, that you can put several
return statements within a method if the logic requires this. If a method does not return a value, you
can just use the keyword return by itself to end execution of the method:
return; // Return from a method
Note that, for methods that do not return a value, falling through the closing brace enclosing the body
of the method is equivalent to executing a return statement.
The Parameter List
The parameter list appears between the parentheses following the method name. This specifies the type
of each value that can be passed as an argument to the method, and the variable name that is to be used
in the body of the method to refer to each value passed. The difference between a parameter and an
argument is sometimes confusing because people often, incorrectly, use them interchangeably. We will
try to differentiate them consistently, as follows:
A parameter has a name and appears in the parameter list in the definition of a method. A
parameter defines the type of value that can be passed to the method, and the name that is
used to reference it within the code for the method.
An argument is a value that is passed to a method when it is executed, and the value of the
argument is referenced by the parameter name during execution of the method.
Search WWH ::




Custom Search