Java Reference
In-Depth Information
parameter, and so on. The type of each argument must be the same as or narrower
than the type of the corresponding parameter.
In our example, since each parameter of procedure drawLine is declared
using keyword int , the corresponding arguments must be integer-valued.
Determining what execution of a procedure call does
Learn to rely entirely on the specification and header of the method to deter-
mine what a method call will do: simply copy the specification but replace each
parameter in it by the value of the corresponding argument. The result is a state-
ment that is equivalent to the call.
Here is an example. Consider this procedure:
/** Draw a rectangle with top-left corner at pixel (tx, ty) with
height h and width w. */
public void drawRect( int tx, int ty, int w, int h) { … }
To figure out what the call
drawRect(20, 30, 25, 40);
does, make a copy of the specification and replace each parameter in that copy
by the value of the corresponding argument:
Draw a rectangle with top-left corner at pixel (20, 30) with
height 40 and width 25.
The general form of a procedure call
A procedure call consists of:
An identifier: the procedure name.
Zero or more arguments, separated by commas and enclosed in parenthe-
ses.
A semicolon.
There are several rules for the number and types of arguments:
A method call has one argument for each parameter of the method.
An argument is an expression, and its type must be the same as or nar-
rower than the type of the corresponding parameter.
If a method call has no arguments, the parentheses are still necessary.
Thus far, the arguments of method calls have just been integers, but any
expression (of a suitable type) could be used. For example, the argument 40
could have been written as:
20+2*10
 
Search WWH ::




Custom Search