When I take an array, something like this:
int anArray[] = new int[5];
//initialize to 0
doStuff(anArray);
//inside doStuff
anArray[3] = 731;
//passes back to main
System.out.println(anArray[3]); // outputs 731
Isn’t this pass by reference? What gives?
Reference types like arrays are still considered pass-by-value because you’re passing a copy of the value of the reference. Note that you’re not passing in the actual reference handle. That is to say, your doStuff method cannot alter the reference to anArray as would be possible in pass by reference semantics. An example should help understand the difference: