Java Reference
In-Depth Information
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 ambigu-
ous call to an overloaded varargs method. For example, consider the following program:
Search WWH ::




Custom Search