|
Methods with a return value |
|
We’ve learned that parameters represent input
to a method -- the flow of information into the method. It’s also possible for a method to have a return
value, which we can think of as the flow of information out of the
method. While a method can have any
number of parameters, it may only have at most one return value. Here are some examples to make these
concepts clearer: -
We have some method that sums the digits of a
positive whole number: its parameter (flow in) would be the whole number, and
its return value, also an integer, would be the sum of the digits (flow out).
-
A method that clears the contents of a Board object: this would require neither any parameters
nor a return value. It just does the
same thing every time it’s invoked on a Board object. -
A method that retrieves the number of rows in a
Board object: the return value in this case would be the
number of rows (an integer), but no parameters are needed. -
The method that corresponds to the last example
is called getRows (and you can guess that there’s a method getCols). When invoking a
method with a return value, we often need to store it in some variable so
that we can use it later in our program.
Take a look at this code segment which creates a 7x5 Board object and then uses the two methods getRows and getCols to obtain
the board dimensions and store them in variables with the help of the
assignment operator: Board b
= new Board(7, 5); int cols = b.getCols(); // the
return value is stored in cols int rows = b.getRows(); // the
return value is stored in rows System.out.println(“Board dimensions: “ + cols + “x” +
rows); The
output of this code would be: Board dimensions: 7x5 Alternatively,
we can output the return values of the methods without storing them in
variables. The method invocations
would now go inside the parentheses of println: Board b = new Board(7, 5); System.out.println(“Board dimensions: “ + b.getCols() + “x” + b.getRows()); The output
of this code segment is identical to the first one, except that here the
dimensions of the board are not stored in any variables. Finally, a
method with no return values is said to return void, which is a keyword in Java.
One such method is turnOn,
which we’ve used before. No assignment
operator goes before the method invocation since its return type is void. Below you will find a
table of all methods in Board class.
The left-hand column contains the type of the return value. In the second column is
the name of the method and a listing of its parameters and their types inside
the parentheses. Make sure that when
you invoke methods you match the parameter and return value types with those
specified in the table. For example,
if you try to execute the statements Board b
= new Board(10, 10); b.turnOn(2.5, 3); the compiler will display the following
error: Error: No 'turnOn' method in 'Board'
with arguments: (double, int) In
essence, the compiler could not match the parameters types: turnOn requires two integer parameters, not one double and an
integer. |