0

I've been reading this section about static methods and about passing arrays by call-by-reference. So my question is: When is it ever a good time to return an array argument as an array as opposed to void?

Thanks in advance!

5
  • 3
    when you need a modified array but want to keep the parameter array May 3, 2012 at 2:17
  • Static methods aren't special there. All objects are passed by "reference".
    – Ry-
    May 3, 2012 at 2:18
  • 1
    You can't pass an argument or result as void in Java. There is no void data type.
    – Hot Licks
    May 3, 2012 at 2:23
  • maybe this post contains your answer May 3, 2012 at 2:28
  • You're first comment is all I need. Thanks :) May 3, 2012 at 2:34

3 Answers 3

4

I've been reading this section about static methods passing arrays by call-by-reference.

If this is a Java article, site, blog, book, whatever, you should probably find a better one. Java doesn't use "call-by-reference" on any parameters. Anyone who says that either doesn't understand Java, or doesn't understand what "call by reference" means.

Java passes all arguments by value. Period.

The point of confusion is that some people don't understand that Objects and arrays in Java are always references. So when you pass an array (for instance) as an argument, you are passing the reference to the array by value. However, this ins NOT a nitpick. Passing a reference by value is semantically very different to true "call by value".

Re the actual quote:

"All of these examples highlight the basic fact that the mechanism for passing arrays in Java is a call by reference mechanism with respect to the contents of the array" (Sedgewick).

I understand what he is saying, given the qualification " with respect to the contents of the array". But calling this "call by reference" is misleading. (And clearly, you were mislead by it, to a certain degree!)

I would also argue that it is technically wrong. The terms "call-by-value", "call-by-reference", "call-by-name" (and so on are) about parameter passing / returning. In this case, the parameter is the array as a whole, not the contents of the array. And at that level, the semantics are clearly NOT call-by-reference. (Assigning a new array reference to the parameter name in the method does not update the array variable in the caller.) The fact that the behaviour is not distinguishable from call-by-reference with respect to the array contents does not make it call-by-reference.


Now to the meat of your question ...

When is it ever a good time to return an array argument as an array as opposed to void?

It is not entirely clear what you mean, but I assume that you are talking about these two alternatives:

    public void method(String arg, String[] result) ...

versus

    public String[] method(String arg) ...

I'd say that the second form is normally preferable because it is easier to understand and to use. Furthermore, the second form allows the method to choose the size of the result array. (With the first form, if the array is too small or too large, there is no way to return the reference to a reallocated array.)

The only cases where the first form should be used are:

  • when the functional requirements for the method depend on it being to update an existing array, or
  • when there is an overarching need to minimize the number of objects that are allocated; e.g. to minimize GC pauses.

The first case might arise if the array is already referenced in other data structures, and finding / updating those references would be difficult. It also might arise if the array is large, and the cost of making a copy would dominate the cost of the real work done by the method.

2
  • It's probably my fault as I'm very inexperienced with communicating using programming jargon. Here's what I actually read. "All of these examples highlight the basic fact that the mechanism for passing arrays in Java is a call by reference mechanism with respect to the contents of the array" (Sedgewick). May 3, 2012 at 2:46
  • Oh, I see what you mean now. I meant to say "I've been reading this section about static methods AND ABOUT passing arrays by call-by-reference." May 3, 2012 at 2:51
0

All parameters passed into a method in java are reference expect the primitive type, so wherever inside or outside the method it just keeps one object storage in memory, Static method are even NOT treated as any special case here. In your case of returning this array or a void type, it would not make any differences.

If you return this array, the returned value is what exactly you just now passed into this method.

-2

WELL, ultimately... passing an array by value is slow. It has to grab a block of memory and copy the array. If the array is only a few bytes in size, its not a big deal. But if its a large chunk of memory, then this will be a slow IO operation. ESPECIALLY if this is happening in a tight loop, it will hurt performance.

Passing by reference will allow you to create a buffer ahead of time and reuse it.

2
  • @SLaks: FALSE. How would it pass an array by reference if the output of the function is an array? You're not passing it the original array pointer. It has to create the array in the function and THEN it passes that. Also, when it passes an value type, it passes by value. I.E. int and float.
    – poy
    May 3, 2012 at 2:45
  • @Slaks - I think you got that wrong. Java ALWAYS passes by value.
    – Stephen C
    May 3, 2012 at 3:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.