Java multidimensional array considered a primitive or an object

风格不统一 提交于 2019-12-09 19:24:44

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!