JDK1.5 - Painful bugs: Solved
Update: The defects have been solved in the latest release of 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. Also I would like to use auto-boxing to enable me to pass int & floats. With generics, varargs, auto-boxing available, I thought I was all set until I tried. I realized the dream is there but JDK 1.5 is still far from realizing the vision.
The code shows an implementation which should, but doesn't compile with jdk1.5.
To compile use javac -source 1.5 MathUtil.java
It also shows a commented implementation (dumbed down version) which compiles. However it doesn't allow me to realize the benefits of varargs in this case.
Also note that the errors messages are ambiguous (what an ambiguous spelling!). For example the first errors says: generic array creation
Sure I can create a generic array if I use: T [] args, as shown in the commented code.
The other two errors messages are ambiguous too.
Here is the sample code with comments:
/** This class demonstrate few limitations in jdk implementation. The objective is * to implement a generic max function which takes any number of Number values and returns * the max. It allows variable arguments so you can say max(1, 2, 3). It supports * autoboxing so you can use 1 instead of Integer.valueOf(1). And it returns the * data of the same type as passes parameter, so type casting is not required, * using generics capability. * * This class doesn't compile. Errors are shown below. * ** MathUtil.java:16: generic array creation * public static*/ public class MathUtil { /* This alternative dumbed down implementation works public staticT 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 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 * T max(T args[]) { assert args.length > 1; T max = args[0]; for(T arg:args) { if(max.compareTo(arg) < 0) { max = arg; } } return max; } */ public static T max(T ... args) { assert args.length > 1; T max = args[0]; for(T arg:args) { if(max.compareTo(arg) < 0) { max = arg; } } return max; } public static void main(String ... args) { /* This alternative dumbed down implementation works assert max(new Integer[] {10, 2, 3, 5}).intValue() == 10; assert max(new Double[] {10.0, 2.0, 3.2, 5.0}).doubleValue() == 10.0; */ assert max(10, 2, 3, 5).compareTo(10) == 0; assert max(10.0, 2.0, 3.2, 5.0).compareTo(10.0) == 0; } }
See more on bugs in jdk1.5 in my next post.
Filed under Java Software |
1 Comment |
Email this Article

















October 17th, 2006 at 7:43 am
j aimerai approfondir mes connaissances en java
November 10th, 2008 at 11:10 pm
our RMI program is not run in jdk1.5. There is some bug in jdk1.5 for rmi. so list all the detail of the bug and dubug of the jdk1.5.