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. مع generics ، varargs السيارات المتاحة للملاكمة ، اعتقدت انني كان جميع المحددة حتى حاولت. I realized the dream is there but JDK 1.5 is still far from realizing the vision. كنت ادرك الحلم ولكن هل هناك 1،5 جدك] لا يزال بعيدا عن تحقيق الرءيه.

The code shows an implementation which should, but doesn’t compile with jdk1.5. ويبين القانون الذي ينبغي ان تنفيذ ، ولكنها لا تجمع مع جدك 1.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. / ** هذه الفئة القليلة تثبت القيود في تنفيذ جدك]. 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. ويعيد * بيانات من نفس النوع كما يمر المعلمه ، وذلك يلقي ظلالا من النوع غير مطلوب ، generics * استخدام القدرة. *  *  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 لا يمكن تطبيقه على (دولي ، دولي ، دولي ، دولي (* ؛ المثال لا (ق) من النوع المتغير (ق) (ر) ان وجود ذلك النوع دولي يتفق مع الحجه * معلمة رسمية الى نوع ر [] * تأكيد ماكس (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 ، مزدوجة) ؛ المثال لا (ق) من النوع المتغير (ق) (ر) ان وجود ذلك النوع الحجه * د ouble يتفق مع معلمة رسمية نوع ر [] * تأكيد ماكس (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 البق في جدك 1.5 in my next post. في وجهتي المقبلة بعد.