Wiki Markup |
---|
The Java compiler type-checks the arguments to a varargs method to ensure that they are of the same type or object reference. However, the compile-time checking is ineffective when two method signatures are used in particular - particularâ{{Object}} and the generic type {{T}} \[[Bloch 2008|AA. Bibliography#Bloch 08]\]. Another requirement for providing strong compile-time type checking of variable argument methods is to be as specific as possible when declaring the type of the method parameter. |
...
Wiki Markup |
---|
Retrofitting old methods containing final array parameters with generically -typed varargs is not always a good idea. This is because, if some method did not accept an argument of a particular type, it maycould be possible to override the compile-time checking so that, with the use of generic varargs, it now compiles cleanly \[[Bloch 2008|AA. Bibliography#Bloch 08]\]. |
...
DCL09-EX1: Varargs signatures using Object
and imprecise generic types are acceptable when, and only when, the body of the method uses no casts or auto-boxing, and compiles without error. For Consider the following example:
Code Block |
---|
Collection<T> assembleCollection(T... args) { Collection<T> result = new HashSet<T>(); // add each argument to the result collection return result; } |
...
Unmindful use of the varargs feature breaks strong compile-time type checking, creates ambiguity, and diminishes code readability.
...