问题
Is int[][] matrix = new int[10][10];
a primitive or is it considered an object? When i send it as a parameter to a function, does it send its reference (like an object) or its value (like a primitive)?
回答1:
Every Java array is an Object. When you pass it as an argument, you pass a copy of the reference to the array.
回答2:
Arrays are objects. Arrays of arrays are also objects. Java doesn't really have multidimensional arrays as such, just support for 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
回答3:
In java, arrays are full blown objects. Having said that, all primitives and object references in java are always passed by value and never by reference. In the case of objects, the object reference is passed by value. The difference between this and passing by reference is subtle but significant.
来源:https://stackoverflow.com/questions/9644896/java-multidimensional-array-considered-a-primitive-or-an-object