Update: The defects have been solved in the latest release of 1.5 Aggiornamento: I difetti sono stati risolti nella versione più recente di 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. Volevo creare una semplice funzione max, qualcosa che volevo fare per molto talvolta, che prende un numero variabile e numero di argomenti comparabili, essetially Integer, Double ecc, e restituisce il massimo dello stesso tipo. Also I would like to use auto-boxing to enable me to pass int & floats. Inoltre desidero utilizzare l'auto-boxing a mi permettono di passare int & galleggianti. With generics, varargs, auto-boxing available, I thought I was all set until I tried. Con farmaci generici, varargs, auto-boxing disponibili, pensavo che mi è stato fino a quando tutti i set ho cercato. I realized the dream is there but JDK 1.5 is still far from realizing the vision. Ho realizzato il sogno, ma non vi è JDK 1,5 è ancora lontana dal realizzare la visione.

The code shows an implementation which should, but doesn’t compile with jdk1.5. Il codice mostra una implementazione che dovrebbe, ma non compilano con jdk1.5.

To compile use javac -source 1.5 MathUtil.java Per compilare usare javac-source 1,5 MathUtil.java

It also shows a commented implementation (dumbed down version) which compiles. Che dimostra anche uno commentato attuazione (dumbed versione), che compila. However it doesn’t allow me to realize the benefits of varargs in this case. Tuttavia, non mi consente di realizzare i vantaggi della varargs in questo caso.

Also note that the errors messages are ambiguous (what an ambiguous spelling!). Nota anche che gli errori sono messaggi ambigui (come, un ambiguo ortografia!). For example the first errors says: generic array creation Per esempio il primo errori dice: la creazione di array generico
Sure I can create a generic array if I use: T [] args, as shown in the commented code. Sicuro che posso creare un array generico se si usa: T [] args, come mostrato nel codice commentato.
The other two errors messages are ambiguous too. Gli altri due messaggi di errori sono troppo ambigue.

Here is the sample code with comments: Ecco il codice di esempio con commenti:

 /** This class demonstrate few limitations in jdk implementation. / ** Questa classe dimostrare alcuni limiti di jdk attuazione. The objective is  *  to implement a generic max function which takes any number of Number values and returns  *  the max. L'obiettivo è di attuare * un generico max funzione che prende un numero qualsiasi di valori Numero * e restituisce il max. It allows variable arguments so you can say max(1, 2, 3). Esso consente variabile argomenti in modo che tu possa dire max (1, 2, 3). It supports  *  autoboxing so you can use 1 instead of Integer.valueOf(1). Sostiene * autoboxing per poter usare 1 invece di Integer.valueOf (1). And it returns the  *  data of the same type as passes parameter, so type casting is not required,  *  using generics capability. E restituisce i dati * dello stesso tipo passa come parametro, in modo tipo casting non è richiesta, utilizzando * capacità di farmaci generici. *  *  This class doesn't compile. * * Questa classe non compilare. Errors are shown below. Errori sono riportati di seguito. *  * * * 
 *  MathUtil.java:16: generic array creation  *  public static * MathUtil.java: 16: generico array creazione * 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 []) in MathUtil non può essere applicato a (int, int, int, int) *; nessun caso (s) di tipo variabile (s) T esistono in modo tale argomento tipo int * conforme al parametro formale di tipo T [] * Affermare 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 []) in MathUtil non può essere applicato a (doppie, doppie, do * uble, doppio); nessun caso (s) di tipo variabile (s) T esistono in modo tale argomento tipo d * ouble conforme al parametro formale di tipo T [] * Valere max (10,0, 2,0, 3,2, 5.0). CompareTo (10.0) == 0;; * ^ * 3 * errori 

*/ * /
public class MathUtil { classe pubblica MathUtil (
/* This alternative dumbed down implementation works / * Questa alternativa dumbed giù attuazione opere
public static public static T max(T args[]) { T max (T args []) (
assert args.length > 1; affermare args.length> 1;
T max = args[0]; T max = args [0];
for(T arg:args) { per (T arg: args) (
if(max.compareTo(arg) < 0) { if (max.compareTo (ARG) <0) (
max = arg; max = ARG;
} )
} )
return max; Max ritorno;
} )
*/ * /

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

public static void main(String … args) { public static void main (String… args) (
/* This alternative dumbed down implementation works / * Questa alternativa dumbed giù attuazione opere
assert max(new Integer[] {10, 2, 3, 5}).intValue() == 10; affermare max (nuovo Integer [] (10, 2, 3, 5)). intValue () == 10;
assert max(new Double[] {10.0, 2.0, 3.2, 5.0}).doubleValue() == 10.0; affermare max (nuovo Double [] (10,0, 2,0, 3,2, 5,0)). doubleValue () == 10,0;
*/ * /

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

See more on Vedere di più su bugs in jdk1.5 bug in jdk1.5 in my next post. nel prossimo post.