Page 921 - AP Computer Science A, 7th edition
P. 921

19. (C) Recall that primitive types and object references are passed by value. This means that copies are made of the actual arguments. Any changes that are made are made to the copies. The actual parameters remain unchanged. Thus, in methods I and II, the parameters will retain their original values and remain unswapped.
To illustrate, for example, why method II fails, consider this piece of code that tests it:
public static void main(String[] args) {
int x = 8, y = 6;
Integer xObject = new Integer(x); Integer yObject = new Integer(y); swap(xObject, yObject);
x = xObject.intValue();
still has value 8
y = yObject.intValue();
still has value 6
...
}
Here are the memory slots before swap is called:
//surprise! //surprise!
 





















































































   919   920   921   922   923