Java Reference
In-Depth Information
Applicability
Injudicioususeofvariablearityparametertypespreventsstrongcompile-timetypecheck-
ing, creates ambiguity, and diminishes code readability.
Variable arity signatures using
Object
and imprecise generic types are acceptable
whenthebodyofthemethodlacksbothcastsandautoboxing,anditalsocompileswithout
error. Consider the following example, which operates correctly for all object types and
type-checks successfully:
<T> Collection<T> assembleCollection(T... args) {
return new HashSet<T>(Arrays.asList(args));
}
Insomecircumstances,itisnecessarytouseavariablearityparameteroftype
Object
.
A good example is the method
java.util.Formatter.format(String format, Ob-
ject... args)
, which can format objects of any type.
Automated detection is straightforward.
Bibliography
[Bloch 2008]
Item 42, “Use Varargs Judiciously”
[Steinberg 2008]
Using the Varargs Language Feature
[Oracle 2011b]
Varargs