The following code is available:

int[] arr1 = {10, 20, 30, 40}; System.out.println(Arrays.deepToString(arr1)); 

Here the compiler produces an error:

incompatible types: int [] cannot be converted to java.lang.Object []

The following code is compiled without errors:

 int[][] arr2 = {{10, 20}, {30, 40}}; System.out.println(Arrays.deepToString(arr1)); 

Why int[] cannot be converted to Object ? Are arrays of primitives not objects? And why is the two-dimensional array of primitives int[][] in Object perfectly converted?

  • If you look at the error text, you will see that it is being cast in Object[] , and not at all in Object . And in this case it cannot, because primitives are not successors Object. - etki
  • And int[][] - is no longer primitive and is inherited from Object ? - darko
  • int[] also not primitive. The JVM looks at what type is contained in the array and whether it is possible to bring to it the type of the contents of the current array. - etki

1 answer 1

All right says.

int[] is an Object , and int[][] is an array of Object s ( Object[] , where each element is an int[] ), that is, an array of arrays, where the internal array is an object. int[] is an array of primitives that cannot auto-box in Integer[] (are you hinting at this in the question?). Confusing, but hopefully understandable.

For primitive one-dimensional arrays there is Arrays.toString(...)