Java multidimensional array viewed as primitive or object

Is it int[][] matrix = new int[10][10];

primitive or considered an object? When I send it as a parameter to a function, is it sending a reference (like an object) or its value (like a primitive)?


source to share

3 answers

Every Java array is an object. When you pass it as an argument, you are passing a copy of the array reference.



Arrays are objects. Arrays of arrays are also objects. Java doesn't really have multidimensional arrays per se, it just supports arrays of arrays.

int [][] foo = {{1}, {2,2}, {3,4,5}};
if (foo instanceof int[][]) { // can only use instanceof with objects
System.out.println(foo.getClass()); // has object methods




In java, arrays are full blown objects. Having said that, all primitives and object references in java are always passed by value and never referenced. In the case of objects, the reference to the object is passed by value. The difference between this and passing by reference is subtle but significant.



All Articles