2

I read that Java passes arrays to functions by reference (sort of) and it allows modification on it which would be reflected on the original array.

Well I'm not sure about the reference part in the above link because someone else said that Java is always pass by value, with no exceptions, ever. However I know for sure that modification on the argument array changes the original array too.

So does that mean that when I return the same array that was passed through arguments, Java would return a duplicate of the original array? and while not doing that and using the original array instead will save me some memory and CPU usage? or does Java duplicate the array in both cases?

For example are the following two functions identical or does the first one save memory and cpu resources?:

public void modifyArray (int[] arr)
{
    for (int i = 0; i < arr.length; i++) arr[i] = i + 1;
}

public int[] modifyArray (int[] arr)
{
    for (int i = 0; i < arr.length; i++) arr[i] = i + 1;
    return arr;
}

Edit:

Just to be more clear, I am only concerned about the performance (especially when dealing with large arrays) and I don't actually need to copy the array, I have an existing code that does this and want to know if removing the retun part would improve performance.

Community
  • 1
  • 1
razz
  • 9,770
  • 7
  • 50
  • 68

5 Answers5

4

Java passes the reference by value. Which means that replacing the array does no harm, i.e.

public void f(String[] a)
{
   a = new String[42];
}

but changing anything in it will change the original array. Deep copying of general objects can be very time consuming and is also difficult in cases where objects contain objects which reference objects ... so that the standard behaviour is to avoid it.

J Fabian Meier
  • 33,516
  • 10
  • 64
  • 142
2

Both functions modify the array that was passed in. What is passed by value is the pointer to the array, so when you operate on the underlying array that it points to, you are changing the array that was passed in. Thus, the second function is returning the same array that was passed in, after making some changes to it.

Now, if you put arr = ... somewhere in the function and then worked on it, you would not modify the array passed in, and what you return would be something different. This is why some say that Java is passing by value. It's just that the value is a pointer in all non-primitive cases.

Tim Jasko
  • 1,532
  • 1
  • 8
  • 12
1

The methods you've provided are identical in a way, that both modify the array that was passed as a parameter. There is no implicit deep copy when returning the array.

However, it's quite a good practice to either return the array/collection that is a deep copy or make it unmodifiable.

you can create the copy of the array by System.arraycopy which should be super fast, or, when using the containers, create an unmodifiable wrapper by Collections.unmodifiableList/Set/etc()

Note: this works only for "1D" of the container, if you have n dimensional array or container, you need to make the deep copy.

Jiri Kremser
  • 12,471
  • 7
  • 45
  • 72
1

Every primitve is passed in Java by value.

Look at this example:

public void test2(int s) {
    s = 2;
    System.out.println(2);
}

If you ran it:

int k = 5;
reporter.test2(k);
System.out.println(k);

Java will pass s by value. It won't affect original value. The output of k will be still 5.

In case of Objects - it's always reference by value. It passess reference to original value and modifies it.

Answring to your question - in second example you just return the reference to original array. Regardless of returning the reference or not - original one will be affected (because arrays are Objects).

mlewandowski
  • 802
  • 7
  • 14
1

You can do that; have a method return the same array that was passed as argument to that method.

But this makes the "interface" of that method a bit harder to understand.

In other words: unless we are talking really large arrays; or methods that are called hundreds of time per second; then you should focus on clear, readable code that doesn't surprise its readers; versus adding a return statement for no good reason.

You see, the caller of your method should know what he is doing. In other words: he is already passing some array to you; why do you think you should return that to him?

GhostCat
  • 137,827
  • 25
  • 176
  • 248