Returning array from method
In my class, I have a method that returns an array like this.
double arrValues[] = ClassX.getValues();
I wonder that, does the array size have any influence on performance. Are there any copy operation for arrValues[] or is 开发者_Go百科it only a reference return ?
In Java, Object types are returned by reference and primitive types are returned by value. Arrays are full blown Objects in Java. Therefore, in your example, the array is returned by reference and no copy is made.
It's only a reference (like everything except the basic types long, int, short, ...) in java is a only reference).
There's no information about the cost of constructing the array (how much that is affected by size is unknown).
Aside from that, what is returned is just a reference to the array constructed in getValues()
, so the act of returning the array has no real performance impact.
精彩评论