JDK1.5 - Painful bugs: Solved jdk1.5 -痛苦的错误:解决
Update: The defects have been solved in the latest release of 1.5更新:缺陷都已解决,在最新发布的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.我想创建一个简单的最大功能,我想做的事相当一段时间,这需要一个变量的数目有多少&可比的论点, essetially整数,双等,并返回最大的同类型。 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.与仿制药, varargs ,自动拳击可用的,我以为我是所有设置,直到我尝试。 I realized the dream is there but JDK 1.5 is still far from realizing the vision.我意识到的梦想是有1.5的JDK ,但还远没有实现的抱负。
The code shows an implementation which should, but doesn’t compile with jdk1.5.该代码显示了一个执行应,但并没有编制与jdk1.5 。
To compile use javac -source 1.5 MathUtil.java编译使用javac源1.5 mathutil.java
It also shows a commented implementation (dumbed down version) which compiles.它也显示了评论的执行情况( dumbed版)编制。 However it doesn’t allow me to realize the benefits of varargs in this case.不过这并不让我认识到的好处varargs在这种情况下。
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.相信我可以建立一个通用的阵列,如果我使用:吨[ ] args ,如图所示,在评论代码。
The other two errors messages are ambiguous too.其他两个错误讯息是含糊不清。
Here is the sample code with comments:这里是示例代码与评论:
/** This class demonstrate few limitations in jdk implementation. / **这个阶层表现出数的限制的JDK的执行情况。 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).它允许变量的论据,所以你可以说,最高( 1 , 2 , 3 ) 。 It supports * autoboxing so you can use 1 instead of Integer.valueOf(1).它支持* autoboxing ,使您可以使用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.它传回的数据*同一类型,作为通参数,所以型铸造,是不是必需的, *使用非专利能力。 * * This class doesn't compile. * *这个阶层并没有编制。 Errors are shown below.错误如下所示。 * * * ** MathUtil.java:16: generic array creation * public static * mathutil.java : 16 :通用阵列创造*公共静态T max(T … args) { * ^ * MathUtil.java:34:吨的最高(吨… 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:最高(吨[ ] )在mathutil不能适用于(诠释,诠释,诠释,诠释) * ;没有,例如( s )的类型变量( )笔存在,使这一论点int类型符合*正式参数T型[ ] *断言最高( 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 *最高(吨[ ] )在mathutil不能适用(双,双,做* uble ,双) ;没有,例如( s )的类型变量( )笔存在,使这一论点D型* ouble符合正式参数T型[ ] *断言最高( 10.0 , 2.0 , 3.2 , 5.0 ) 。 compareto ( 10.0 ) == 0 ; ; * ^ * 3 *错误 */ * /
public class MathUtil {公共类mathutil (
/* This alternative dumbed down implementation works / *这个方案, dumbed下来的实施工程
public static公共静态T max(T args[]) {吨的最高(吨args [ ] ) (
assert args.length > 1;断言args.length > 1 ;
T max = args[0];吨的最高= args [ 0 ] ;
for(T arg:args) { (吨阿根廷: args ) (
if(max.compareTo(arg) < 0) {如果( max.compareto (阿根廷) < 0 ) (
max = arg;最高=精氨酸;
} )
} )
return max;回报最高;
} )
*/ * /public static公共静态
T max(T … args) {吨的最高(吨… args ) (
assert args.length > 1;断言args.length > 1 ;
T max = args[0];吨的最高= args [ 0 ] ;
for(T arg:args) { (吨阿根廷: args ) (
if(max.compareTo(arg) < 0) {如果( max.compareto (阿根廷) < 0 ) (
max = arg;最高=精氨酸;
} )
} )
return max;回报最高;
} )public static void main(String … args) {公共静态无效的主要(字符串… args ) (
/* This alternative dumbed down implementation works / *这个方案, dumbed下来的实施工程
assert max(new Integer[] {10, 2, 3, 5}).intValue() == 10;断言最高(新的整数[ ] ( 10 , 2 , 3 , 5 ) ) 。 intvalue ( ) == 10 ;
assert max(new Double[] {10.0, 2.0, 3.2, 5.0}).doubleValue() == 10.0;断言最高(新双[ ] ( 10.0 , 2.0 , 3.2 , 5.0 ) ) 。 doublevalue ( ) == 10.0 ;
*/ * /assert max(10, 2, 3, 5).compareTo(10) == 0;断言最高( 10 , 2 , 3 , 5 ) 。 compareto ( 10 ) == 0 ;
assert max(10.0, 2.0, 3.2, 5.0).compareTo(10.0) == 0;断言最高( 10.0 , 2.0 , 3.2 , 5.0 ) 。 compareto ( 10.0 ) == 0 ;
} )
} )
See more on看到更多的就 bugs in jdk1.5臭虫在jdk1.5 in my next post.在我的下一个职位。
Filed under提起下 Java Software Java软件 | |
| |
RSS 2.0 2.0 | |
Email this Article电子邮件此文章
You may also like to read您也可以想读 |




October 17th, 2006 at 7:43 am 2006年10月17日在上午07时43分
j aimerai approfondir mes connaissances en java j aimerai approfondir MES的connaissances恩华