T[] is not applicable for int[] in Java

1 min read

Type cast involving arrays is weird in Java. When I do this, I got an error.

int[] arr = { 4, 9, 3, 1 }; // error
var i = Q2.check(arr);

The check method looks like this:

public static <T extends Comparable<T>> boolean check(T[] arr) {
    var copy = Arrays.copyOf(arr, arr.length);
 
    Shell.sort(arr);
    Arrays.sort(copy);
 
    return Arrays.equals(arr, copy);
}

"The method check(T[]) in the type Q2 is not applicable for the arguments (int[])," the compiler complains.

And I have to change int to Integer in order to make it work. I guess Java just handles int and Integer differently.

Integer[] arr = { 4, 9, 3, 1 };
var i = Q2.check(arr);
CC BY-NC 4.0 2023 © donaldnevermore.RSS