penguru
penguru

Reputation: 4380

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

Upvotes: 1

Views: 232

Answers (3)

Asaph
Asaph

Reputation: 162851

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.

Upvotes: 3

Nader Shirazie
Nader Shirazie

Reputation: 10776

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.

Upvotes: 3

tangens
tangens

Reputation: 39753

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

Upvotes: 3

Related Questions