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.