tags:

views:

73

answers:

3

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 it only a reference return ?

+3  A: 

It's only a reference (like everything except the basic types long, int, short, ...) in java is a only reference).

tangens
+3  A: 

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.

Nader Shirazie
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 C
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 Shirazie
getValues() only returns the array that is already constructed before. I wanted to ask only the cost of returning the array.
penguru
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 Shirazie
+3  A: 

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.

Asaph
+1 for _why_ it is a reference rather than just stating it is a reference
Nader Shirazie