Update: The defects have been solved in the latest release of 1.5 Actualización: Los defectos se han resuelto en la última versión de 1,5

I wanted to create a simple max function, something I wanted to do for quite sometime, which takes a variable number of Number & Comparable arguments, essetially Integer, Double etc., and returns the max of the same type. Quería crear una simple función max, algo que yo quería hacer desde hace algún momento, que tiene un número variable de Número y argumentos comparables, essetially Integer, Double, etc, y devuelve el máximo del mismo tipo. Also I would like to use auto-boxing to enable me to pass int & floats. También quisiera aprovechar auto-boxeo para permitir que me pase a int y carrozas. With generics, varargs, auto-boxing available, I thought I was all set until I tried. Con los genéricos, VarArgs, auto-boxeo disponible, pensé que era todo conjunto hasta que me trataron. I realized the dream is there but JDK 1.5 is still far from realizing the vision. Me di cuenta el sueño está ahí pero JDK 1,5 es todavía muy lejos de hacer realidad la visión.

The code shows an implementation which should, but doesn’t compile with jdk1.5. El código muestra una aplicación que debería, pero no compila con el jdk1.5.

To compile use javac -source 1.5 MathUtil.java Para compilar uso javac-source 1,5 MathUtil.java

It also shows a commented implementation (dumbed down version) which compiles. También comentó muestra una aplicación (dumbed versión reducida) que compila. However it doesn’t allow me to realize the benefits of varargs in this case. Sin embargo, no me permite obtener los beneficios de VarArgs en este caso.

Also note that the errors messages are ambiguous (what an ambiguous spelling!). También tenga en cuenta los errores que los mensajes son ambiguos (lo ambiguo de ortografía!). For example the first errors says: generic array creation Por ejemplo el primer errores dice: array genérico creación
Sure I can create a generic array if I use: T [] args, as shown in the commented code. Seguro de que puedo crear una matriz genérica si uso: T [] args, como se indica en el código comentado.
The other two errors messages are ambiguous too. Los otros dos errores en los mensajes son ambiguos.

Here is the sample code with comments: Aquí está el código de ejemplo con comentarios:

 /** This class demonstrate few limitations in jdk implementation. / ** Esta clase demostrar algunas limitaciones en jdk aplicación. The objective is  *  to implement a generic max function which takes any number of Number values and returns  *  the max. El objetivo es * para poner en marcha un genérico max función que toma cualquier número de Número de valores y devuelve el * max. It allows variable arguments so you can say max(1, 2, 3). Permite que los argumentos variable por lo que puede decir max (1, 2, 3). It supports  *  autoboxing so you can use 1 instead of Integer.valueOf(1). Apoya * autoboxing así que usted puede utilizar en lugar de 1 Integer.valueOf (1). And it returns the  *  data of the same type as passes parameter, so type casting is not required,  *  using generics capability. Y devuelve los datos * del mismo tipo que pasa el parámetro, por lo que el tipo de fundición no se requiere *, utilizando la capacidad de los genéricos. *  *  This class doesn't compile. * * Esta clase no compila. Errors are shown below. Los errores se muestran a continuación. *  * * * 
 *  MathUtil.java:16: generic array creation  *  public static * MathUtil.java: 16: array genérico creación * public static  T max(T … args) {  *                                                   ^  *  MathUtil.java:34: T max (T… args) (* ^ * MathUtil.java: 34:  max(T[]) in MathUtil cannot be applied to (int,int,int,int)  *  ; no instance(s) of type variable(s) T exist so that argument type int conforms  *  to formal parameter type T[]  *          assert max(10, 2, 3, 5).compareTo(10) == 0;;  *                 ^  *  MathUtil.java:35: max (T []) en MathUtil no se puede aplicar a (int, int, int, int) *; ningún caso (s) de tipo variable (s) T existe argumento para que el tipo int * ajusta a los parámetros formales de tipo T [] * Afirmar max (10, 2, 3, 5). CompareTo (10) == 0; * ^ * MathUtil.java: 35:  max(T[]) in MathUtil cannot be applied to (double,double,do  *  uble,double); no instance(s) of type variable(s) T exist so that argument type d  *  ouble conforms to formal parameter type T[]  *          assert max(10.0, 2.0, 3.2, 5.0).compareTo(10.0) == 0;;  *                 ^  *  3 errors  * max (T []) en MathUtil no se puede aplicar a (doble, doble, ¿* uble, doble); ningún caso (s) de tipo variable (s) T existe argumento para que tipo d * ouble se ajusta a los parámetros formales de tipo T [] * Valer max (10,0, 2,0, 3,2, 5,0). CompareTo (10,0) == 0; * ^ * 3 * errores 

*/ * /
public class MathUtil { MathUtil público de clase (
/* This alternative dumbed down implementation works / * Esta alternativa dumbed abajo ejecución de obras
public static public static T max(T args[]) { T max (T args []) (
assert args.length > 1; afirmar args.length> 1;
T max = args[0]; T max = args [0];
for(T arg:args) { (T arg: args) (
if(max.compareTo(arg) < 0) { if (max.compareTo (arg) <0) (
max = arg; Max = arg;
} )
} )
return max; Max retorno;
} )
*/ * /

public static public static T max(T … args) { T max (T… args) (
assert args.length > 1; afirmar args.length> 1;
T max = args[0]; T max = args [0];
for(T arg:args) { (T arg: args) (
if(max.compareTo(arg) < 0) { if (max.compareTo (arg) <0) (
max = arg; Max = arg;
} )
} )
return max; Max retorno;
} )

public static void main(String … args) { public static void main (String… args) (
/* This alternative dumbed down implementation works / * Esta alternativa dumbed abajo ejecución de obras
assert max(new Integer[] {10, 2, 3, 5}).intValue() == 10; afirmar max (nuevo Integer [] (10, 2, 3, 5)). intValue () == 10;
assert max(new Double[] {10.0, 2.0, 3.2, 5.0}).doubleValue() == 10.0; afirmar max (nuevo doble [] (10,0, 2,0, 3,2, 5,0)). doubleValue () == 10,0;
*/ * /

assert max(10, 2, 3, 5).compareTo(10) == 0; afirmar max (10, 2, 3, 5). compareTo (10) == 0;
assert max(10.0, 2.0, 3.2, 5.0).compareTo(10.0) == 0; afirmar max (10,0, 2,0, 3,2, 5,0). compareTo (10,0) == 0;
} )
} )

See more on Ver más en bugs in jdk1.5 errores en jdk1.5 in my next post. en mi próximo post.