Java Reference
In-Depth Information
The above call to the
add
method passes 10 and 12 as the values for parameters
n1
and
n2
, respectively. The two
values, 10 and 12, that are used to call the
add
method are called actual parameters. Java copies the actual parameters
to the formal parameters before it executes the code inside the body of the method. In the above call to the
add
method, 10 will be copied in
n1
, and 12 will be copied in
n2
. You can refer to the formal parameter names as variables
having actual parameter values inside the method's body. You can see
n1
and
n2
being treated as variables in the
following statement in the
add
method:
int sum = n1 + n2;
A
return
statement is used to return a value from a method. It starts with the
return
keyword. If a method
returns a value, the
return
keyword must be followed by an expression, which evaluates to the value being returned.
If the method does not return a value, its return type is specified as
void
. If the method's return type is
void
, the
method does not have to include a
return
statement. If a method with a
void
return type wants to include a
return
statement, the
return
keyword must not be followed by any expression; the
return
keyword is immediately followed
by a semicolon to mark the end of the statement. Here are the two flavors of the
return
statement:
// If a method returns a value, <<an expression>>must evaluate to a data type,
// which is assignment compatible with the specified return type of the method
return <<an expression>>;
or
// If method's return type is void
return;
What does a
return
statement do? As its name suggests, it returns the control to the caller of the method. If it has
an expression, it evaluates the expression and returns the value of the expression to the caller. If a
return
statement
does not have an expression, it simply returns the control to its caller. A
return
statement is the last statement that is
executed in a method's body. You can have multiple
return
statements in a method's body. However, at most, only
one
return
statement may be executed for a method call.
The
add
method returns the sum of two of its parameters. How do you capture the returned value of a method?
A method call itself is an expression whose data type is the return type of the method and it evaluates to the returned
value from the method. For example, if you write a statement like
add(10, 12);
add(10, 12)
is an expression and its data type is
int
. At runtime, it will be evaluated to an
int
value of 22,
which is the value returned from the
add
method. To capture the value of a method call, you can use the method call
expression anywhere you can use a value. For example, the following snippet of code assigns the value returned from
the
add
method to a variable call
sum
:
int sum = add(10, 12); // sum variable will be assigned 22
Now turn your attention to a method, which does not return a value. You specify
void
as the return type for such
a method. Let's consider the following method declaration for a method
printPoem
;
void printPoem() {
System.out.println("Strange fits of passion have I known:");
System.out.println("And I will dare to tell,");
System.out.println("But in the lover's ear alone,");
System.out.println("What once to me befell.");
}