JDK1.5 - Painful bugs: Solved JDK1.5 - doloroso bugs: Resolvido
Update: The defects have been solved in the latest release of 1.5 Update: Os defeitos foram resolvidos na última liberação 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. Eu queria criar uma simples função max, algo que queria fazer por algum tempo bastante, o que leva um número variável de argumentos Número e comparáveis, essetially Integer, Double etc, e retorna o máximo do mesmo tipo. Also I would like to use auto-boxing to enable me to pass int & floats. Também gostaria de utilização de auto-boxe para permitir-me para passar int e carros alegóricos. With generics, varargs, auto-boxing available, I thought I was all set until I tried. Com genéricos, VarArgs, auto-boxing disponíveis, pensei que estava tudo definir até que eu tentei. I realized the dream is there but JDK 1.5 is still far from realizing the vision. Eu percebi o sonho está lá, mas JDK 1,5 está ainda longe de realizar a visão.
The code shows an implementation which should, but doesn’t compile with jdk1.5. O código revela uma execução, que deveria, mas não compilar com jdk1.5.
To compile use javac -source 1.5 MathUtil.java Para compilar use javac-source 1,5 MathUtil.java
It also shows a commented implementation (dumbed down version) which compiles. Também mostra uma comentou execução (dumbed versão), que compila. However it doesn’t allow me to realize the benefits of varargs in this case. No entanto, isso não me permite perceber os benefícios da VarArgs neste caso.
Also note that the errors messages are ambiguous (what an ambiguous spelling!). Observe também que os erros são mensagens ambíguas (qual um ambíguo ortografia!). For example the first errors says: generic array creation Por exemplo, o primeiro erro diz: array genérico criação
Sure I can create a generic array if I use: T [] args, as shown in the commented code. Claro que posso criar um array genérico se eu usar: T [] args, como mostrado no código comentou.
The other two errors messages are ambiguous too. Os outros dois erros mensagens são demasiado ambígua.
Here is the sample code with comments: Aqui está a amostra com código comentários:
/** This class demonstrate few limitations in jdk implementation. / ** Esta classe demonstrar algumas limitações na implementação JDK. The objective is * to implement a generic max function which takes any number of Number values and returns * the max. O objetivo é implementar um genérico * a função max que toma qualquer número de valores e retorna * Número do max. It allows variable arguments so you can say max(1, 2, 3). Este mecanismo permite que os argumentos variável para que você possa dizer max (1, 2, 3). It supports * autoboxing so you can use 1 instead of Integer.valueOf(1). Ele suporta * autoboxing assim você pode usar em vez 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. E ele retorna a * dados do mesmo tipo que passa parâmetro, de modo tipo vazamento não é exigida, * usando genéricos capacidade. * * This class doesn't compile. * * Esta classe não compilar. Errors are shown below. Os erros são mostrados abaixo. * * * ** MathUtil.java:16: generic array creation * public static * MathUtil.java: 16: array genérico criação público estático *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 []) no MathUtil não pode ser aplicado a (int, int, int, int) *; nenhuma instância (s) do tipo variável (s) de modo que existem T argumento tipo int * conforma ao parâmetro formal tipo T [] * Valer 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 []) no MathUtil não pode ser aplicado a (double, double, fazer * uble, duplo); nenhuma instância (s) do tipo variável (s) de modo que existem T argumento tipo d * ouble está de acordo com parâmetros formais tipo T [] * Valer max (10,0, 2,0, 3,2, 5,0). CompareTo (10,0) == 0;; ^ * * * 3 erros */ * /
public class MathUtil { público classe MathUtil (
/* This alternative dumbed down implementation works / * Esta alternativa dumbed estabelece execução obras
public static public staticT max(T args[]) { T max (T args []) (
assert args.length > 1; valer args.length> 1;
T max = args[0]; T max = args [0];
for(T arg:args) { para (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; valer args.length> 1;
T max = args[0]; T max = args [0];
for(T arg:args) { para (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 estabelece execução obras
assert max(new Integer[] {10, 2, 3, 5}).intValue() == 10; valer max (novo Integer [] (10, 2, 3, 5)). intValue () == 10;
assert max(new Double[] {10.0, 2.0, 3.2, 5.0}).doubleValue() == 10.0; valer max (novo duplo [] (10,0, 2,0, 3,2, 5,0)). doubleValue () == 10,0;
*/ * /assert max(10, 2, 3, 5).compareTo(10) == 0; valer max (10, 2, 3, 5). compareTo (10) == 0;
assert max(10.0, 2.0, 3.2, 5.0).compareTo(10.0) == 0; valer max (10,0, 2,0, 3,2, 5,0). compareTo (10,0) == 0;
} )
} )
See more on Veja mais sobre bugs in jdk1.5 bugs no jdk1.5 in my next post. no meu próximo post.
Filed under Arquivado em Java Software Java Software | |
| |
RSS 2.0 RSS 2,0 | |
Email this Article E-mail este artigo
You may also like to read Você pode também gosta de ler |





October 17th, 2006 at 7:43 am Outubro 17o, 2006 em 7:43 am
j aimerai approfondir mes connaissances en java j aimerai approfondir mes connaissances en java