3 Answers
3

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.

In the absence of hardware assistance, and assuming a good GC, the amortized cost of allocating an N word array is the O(N) memory writes required to zero it.
–
Stephen COct 23 '09 at 7:24

I think I know what you mean, and I don't disagree. However, we don't know what the implementation of getValues() does. The "cost" I was talking about was not referring to the cost of doing "new Array[X]", rather its the cost of calculating the values that go into the array. How that scales... I don't know. However, that may not be the question the OP is asking...
–
Nader ShirazieOct 23 '09 at 13:59

getValues() only returns the array that is already constructed before. I wanted to ask only the cost of returning the array.
–
penguruOct 26 '09 at 13:28

Ok, cool. It is usually a good question to ask because, very often, cached array's aren't returned (because the caller can then modify it). Usually a copy of an array is returned, or a new array is constructed. These are just valuable questions to answer when looking at performance.
–
Nader ShirazieOct 26 '09 at 16:32