Java Reference
In-Depth Information
make the white icing, and then continue with the chocolate cake recipe.
Similarly, Java methods can contain instructions to carry out other methods. In
Java, an instruction that pauses the current method and executes another method
is called a method call or invocation.
In Java, we say that a method is being executed while the computer (or you)
is carrying it out.
Rombauer and Becker's Joy of Cooking has a recipe for Chocolate Apricot
Cake. This recipe merely says to make another recipe, for chocolate prune cake,
but to substitute apricots for the prunes. Thus, the call of the prune cake recipe
asks for a substitution of one ingredient for another:
Chocolate Apricot Cake
Follow the recipe for: Chocolate Prune Cake, but
Substitute for the prunes: 1 cup cooked pureed apricots.
Omit the spices
This substitution of ingredients in recipes, with the substitution being indicated
at the call on the recipe, is an important concept —much more so in program-
ming than in cooking! We illustrate how the concept works in terms of recipes.
First, let's write a recipe for Chocolate X Cake , where X is a fruit to be
named later. X is called a parameter of the recipe.
Chocolate (X) Cake
Sift: 1 1/2 cups cake flour
Resift with: 1/2 t baking soda, ...
Add: 1 cup X
// Note the use of parameter X here
To use this recipe to make a chocolate prune cake, we use the instruction
Make Chocolate (prunes) Cake .
Executing this instruction results in following the Chocolate (X) Cake recipe
with prunes substituted for X . We can use the same recipe to make an apricot cake
as well:
Make Chocolate (apricots) Cake .
The Chocolate (X) Cake recipe is a parameterized recipe, with X being the
parameter. In the same way, methods in Java are parameterized sequences of
We recommend that you reread this section (Sec. 2.1) again in a week, espe-
cially if you are confused about parameters and method calls in Java.
Search WWH ::

Custom Search