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恩華