System, but they may not be reproduced for publication



Yüklə 83 Mb.
Pdf görüntüsü
səhifə34/82
tarix19.04.2023
ölçüsü83 Mb.
#106251
1   ...   30   31   32   33   34   35   36   37   ...   82
Java A Beginner’s Guide, Eighth Edition ( PDFDrive )

varargs method. The parameter list for a varargs method is not fixed, but rather
variable in length. Thus, a varargs method can take a variable number of arguments.
Varargs Basics
A variable­length argument is specified by three periods (...). For example, here is how
to write a method called vaTest( ) that takes a variable number of arguments:
Notice that v is declared as shown here:
This syntax tells the compiler that vaTest( ) can be called with zero or more
arguments. Furthermore, it causes v to be implicitly declared as an array of type int[ ].
Thus, inside vaTest( ), v is accessed using the normal array syntax.
Here is a complete program that demonstrates vaTest( ):


The output from the program is shown here:
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 arguments 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:
The output from this program is shown here:


Remember, the varargs parameter must be last. For example, the following declaration
is incorrect:
Here, there is an attempt to declare a regular parameter after the varargs parameter,
which is illegal. There is one more restriction to be aware of: there must be only one
varargs parameter. For example, this declaration is also invalid:
The attempt to declare the second varargs parameter is illegal.
Overloading Varargs Methods
You can overload a method that takes a variable­length argument. For example, the
following program overloads vaTest( ) three times:


The output produced by this program is shown here:


This program illustrates both ways that a varargs method can be overloaded. First, the
types of its vararg parameter can differ. This is the case for vaTest(int ...) and
vaTest(boolean ...). Remember, the ... causes the parameter to be treated as an array
of the specified type. Therefore, just as you can overload methods by using different
types of array parameters, you can overload varargs methods by using different types of
varargs. In this case, Java uses the type difference to determine which overloaded
method to call.
The second way to overload a varargs method is to add one or more normal parameters.
This is what was done with vaTest(String, int ...). In this case, Java uses both the
number of arguments and the type of the arguments to determine which method to call.
Varargs and Ambiguity
Somewhat unexpected errors can result when overloading a method that takes a
variable­length argument. These errors involve ambiguity because it is possible to
create an ambiguous call to an overloaded varargs method. For example, consider the
following program:


In this program, the overloading of vaTest( ) is perfectly correct. However, this
program will not compile because of the following call:
Because the vararg parameter can be empty, this call could be translated into a call to
vaTest(int ...) or to vaTest(boolean ...). Both are equally valid. Thus, the call is
inherently ambiguous.
Here is another example of ambiguity. The following overloaded versions of vaTest( )
are inherently ambiguous even though one takes a normal parameter:
Although the parameter lists of vaTest( ) differ, there is no way for the compiler to
resolve the following call:
vaTest(1)


Does this translate into a call to vaTest(int ...), with one varargs argument, or into a
call to vaTest(int, int ...) with no varargs arguments? There is no way for the
compiler to answer this question. Thus, the situation is ambiguous.
Because of ambiguity errors like those just shown, sometimes you will need to forego
overloading and simply use two different method names. Also, in some cases,
ambiguity errors expose a conceptual flaw in your code, which you can remedy by more
carefully crafting a solution.

Yüklə 83 Mb.

Dostları ilə paylaş:
1   ...   30   31   32   33   34   35   36   37   ...   82




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©genderi.org 2024
rəhbərliyinə müraciət

    Ana səhifə