Dayaan M. answered 5d
Computer Science Graduate with Computer Engineering Coursework
Short answer: yes, the change sticks. A method that assigns to an element of an array parameter really does change the caller's array. But the usual one line reason for it, "arrays are passed by reference," is not quite accurate, and the inaccuracy will bite you on the very next question. Here is the precise version.
What Java actually does.
Java is always pass by value. Every argument is copied. For an array, the thing being copied is not the array itself, it is the reference, the arrow that points at the array object sitting in memory.
So after the call there are two arrows, the caller's and the method's, both pointing at the same one array. The array was never duplicated.
That single fact explains both of the behaviors below.
Case 1: changing an element. This is visible to the caller.
public static void changeNames(String[] names) {
names[0] = "Changed";
}
The method follows its arrow to the array and overwrites slot 0. There is only one array, so the caller sees "Changed" when it looks at names[0]. Nothing was copied back, there was nothing to copy back.
Case 2: reassigning the parameter. This is NOT visible to the caller.
public static void reassign(String[] names) {
names = new String[] {"Brand", "New"};
}
This one looks like it should work and it does nothing. The method points its own arrow at a brand new array. The caller's arrow never moved, so the caller still sees the original array, unchanged.
This is exactly where "passed by reference" misleads people. If arrays were truly passed by reference, Case 2 would change the caller's variable. It does not.
Run it yourself and watch both.
public class Demo {
public static void changeNames(String[] names) { names[0] = "Changed"; }
public static void reassign(String[] names) { names = new String[] {"Brand", "New"}; }
public static void main(String[] args) {
String[] names = {"Ann", "Ben", "Cal"};
changeNames(names);
System.out.println(names[0]); // Changed
reassign(names);
System.out.println(names[0]); // still Changed
}
}
I compiled and ran this before posting. The output is "Changed" then "Changed". The second call had no effect at all.
The mental model that makes this stick.
Think of the reference as a house address written on a slip of paper. Passing the array hands the method a photocopy of the slip.
The method can drive to that address and repaint the house. You come home to a repainted house. That is Case 1.
The method can also scribble a different address on its photocopy. Your slip still says the old address, and your house is untouched. That is Case 2.
The practical takeaway.
If you want a method to modify the contents of an array, assigning to elements works and is the normal way to do it. If you want a method to hand back a different or resized array, you cannot do it by assigning to the parameter. Return the new array and let the caller store it:
names = resize(names);
And this is not special to arrays. It is true of every object in Java, ArrayList, String, your own classes. Mutating the object through the parameter is visible to the caller. Reassigning the parameter never is.