I'm attempting to call a method that returns the identity of a generic array, supplying arrays of ints and doubles as test data. My typed parameters should implement the Comparable interface, but the compiler seems to complain:
method identity in class com.me.Test cannot be applied to given types;
required: T[]
found: int[]
reason: inference variable T has incompatible bounds
equality constraints: int
lower bounds: java.lang.Comparable<T>
I'm new to Java, so I'm probably missing something. Any thoughts?
Code:
public class Main {
public static void main(String[] args) {
int[] arr1 = {1};
double[] arr2 = {1.0};
System.out.println(Test.identity(arr1));
System.out.println(Test.identity(arr2));
}
}
public class Test {
public static <T extends Comparable<T>> T[] identity(T[] x) {
return x;
}
}
Integer[]andDouble[], respectively.