Java Reference
In-Depth Information
There are two important things to notice about this program. First, as explained, inside
vaTest( )
,
v
is operated on as an array. This is because
v
is an array
. The … syntax simply
tells the compiler that a variable number of arguments will be used, and that these argu-
ments will be stored in the array referred to by
v
. Second, in
main( )
,
vaTest( )
is called
with different numbers of arguments, including no arguments at all. The arguments are
automatically put in an array and passed to
v
. In the case of no arguments, the length of the
array is zero.
A method can have “normal” parameters along with a variable-length parameter.
However, the variable-length parameter must be the last parameter declared by the method.
For example, this method declaration is perfectly acceptable:
In this case, the first three arguments used in a call to
doIt( )
are matched to the first three
parameters. Then, any remaining arguments are assumed to belong to
vals
.
Here is a reworked version of the
vaTest( )
method that takes a regular argument and a
variable-length argument: