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. 또한 자동차 - 권투를 사용하려는 나는 나에게 패스 int & 부동 소수점을 사용합니다. 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. 나는 꿈의 실현은 jdk 1.5는 아직 멀리 떨어져 있지만 실현의 비전합니다.

The code shows an implementation which should, but doesn’t compile with jdk1.5. 이 코드를 보여줍니다을 구현해야하지만 싶다 컴파일되지 않습니다.

To compile use javac -source 1.5 MathUtil.java 1.5을 사용 소스를 컴파일 javac - 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. 물론 일반적인 배열을 만들 수있습니다 사용하는 경우 : t [] 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 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: t 맥스 (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: 맥스 (t [])에서 mathutil를 적용할 수없습니다 (int, int, int, int) *; 아니오 인스턴스 (들)의 유형 변수 (들) t 존재하지 않으므로 인수 정수형을 준수하는 공식적인 매개 변수 유형 * 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  * 맥스 (t [])에서 mathutil를 적용할 수없습니다 (더블, 더블로하지 * uble, 더블); 아니오 인스턴스 (들)의 유형 변수 (들)이 존재하지 않으므로 t 인수 유형 패 * 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[]) { t 맥스 (t args []) (
assert args.length > 1; 주장 args.length> 1;
T max = args[0]; t 맥스 = args [0];
for(T arg:args) { 을 (t arg : args) (
if(max.compareTo(arg) < 0) { 만일 (max.compareto (arg) <0) (
max = arg; 맥스 = arg;
} )
} )
return max; 반환 맥스;
} )
*/ * /

public static 공공 정체 T max(T … args) { t 맥스 (t… args) (
assert args.length > 1; 주장 args.length> 1;
T max = args[0]; t 맥스 = args [0];
for(T arg:args) { 을 (t arg : args) (
if(max.compareTo(arg) < 0) { 만일 (max.compareto (arg) <0) (
max = arg; 맥스 = 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 버그가 싶다 in my next post. 내 다음에 게시합니다.