El JLS especifica este comportamiento (creación de un conjunto de elementos del tipo que es la aridad tipo de parámetro variable, es decir, si el método vararg es foo(Bar bar, Baz baz, T...)
entonces la matriz creada en la invocación del método es de tipo T[]
), si encuentra el lugar correcto:
de JLS 8.4.1 (Oracle sitio tiene problemas en este momento, tuve que usar el Archivo de Internet):
If the last formal parameter is a variable arity parameter of type T, it is considered to define a formal parameter of type T[]. The method is then a variable arity method. Otherwise, it is a fixed arity method. Invocations of a variable arity method may contain more actual argument expressions than formal parameters. All the actual argument expressions that do not correspond to the formal parameters preceding the variable arity parameter will be evaluated and the results stored into an array that will be passed to the method invocation (§15.12.4.2).
de JLS 15.12.4.2:
15.12.4.2 Evaluate Arguments The process of evaluating of the argument list differs, depending on whether the method being invoked is a fixed arity method or a variable arity method (§8.4.1).
If the method being invoked is a variable arity method (§8.4.1) m, it necessarily has n>0 formal parameters. The final formal parameter of m necessarily has type T[] for some T, and m is necessarily being invoked with k >= 0 actual argument expressions.
If m is being invoked with k != n actual argument expressions, or, if m is being invoked with k=n actual argument expressions and the type of the kth argument expression is not assignment compatible with T[], then the argument list (e1, ... , en-1, en, ...ek) is evaluated as if it were written as (e1, ..., en-1, new T[]{en, ..., ek}).
The argument expressions (possibly rewritten as described above) are now evaluated to yield argument values. Each argument value corresponds to exactly one of the method's n formal parameters.
The argument expressions, if any, are evaluated in order, from left to right. If the evaluation of any argument expression completes abruptly, then no part of any argument expression to its right appears to have been evaluated, and the method invocation completes abruptly for the same reason.The result of evaluating the jth argument expression is the jth argument value, for 1 <= j <= n. Evaluation then continues, using the argument values, as described below.
Por lo tanto, mantengo mi respuesta original (ver a continuación).
Creo que la respuesta está en la declaración:
public static Object combine(Object... objs)
El compilador coincide con este método, y por lo tanto para varargs se asigna una Object[]
. No hay ninguna razón para asignar un Integer[]
.
prueba ensayo:
package com.example.test;
public class Varargs1 {
public static void varargs(Object... objs) {
System.out.println(objs.getClass());
}
public static void main(String[] args) {
varargs("1", "2", "3");
varargs(1, 2, 3);
Integer[] ints = {1,2,3};
varargs(ints); // Eclipse yields the following warning:
/*
* The argument of type Integer[] should explicitly be
* cast to Object[] for the invocation of the varargs
* method varargs(Object...) from type Varargs1.
* It could alternatively be cast to Object for a
* varargs invocation
*/
}
}
que imprime:
class [Ljava.lang.Object;
class [Ljava.lang.Object;
class [Ljava.lang.Integer;
Por último, si desea el compilador para ser más específicos, utilizar métodos genéricos:
package com.example.test;
public class Varargs2 {
public static <T> void varargs(T... objs) {
System.out.println(objs.getClass());
}
public static void main(String[] args) {
varargs("1", "2", "3");
varargs(1, 2, 3);
varargs(1, "2", 3); // warning from Eclipse:
/*
* Type safety : A generic array of
* Object&Comparable<?>&Serializable
* is created for a varargs parameter
*/
}
}
que imprime:
class [Ljava.lang.String;
class [Ljava.lang.Integer;
class [Ljava.lang.Comparable;
posible duplicado de [Java: resolución de método sobrecargado y varargs - ejemplo confuso] (http://stackoverflow.com/questions/6032901/java-overloaded-method-resolution-and-varargs -confusing-example) –
He visto esa pregunta (¡y la he votado a favor!) Sin embargo, esa pregunta se refiere a la sobrecarga, ¿cuál es un problema completamente diferente según lo veo? – aioobe
De acuerdo, es un problema diferente. Esta pregunta es sobre el paquete del compilador de la matriz varargs, no sobre la resolución del método. –