References are passed by values: http://javadude.com/articles/passbyvalue.htm
Let explain by a metaphore:
Suppose a red button.
When I click on the red button, a ball appears.
Now, in order to have quickly more balls, a new button is assigned the same function: the same ball appears when clicked.
Then, a person comes and alters this new button, to make an other ball appear!
What do you expect about the first button? To make appear the original one or the new one?
The original one of course! Since, although buttons pointed to the same ball, they are totally independent, explaining why change applying on the second button does not alter the objective of the first one.
Only if the second button MUTATED the first ball (without changing it!), for instance painting it in blue instead of red, then yes, the first button would point to the same ball blue, since it's..the same!
Replace original button by your a
or b
references and the ball by the x
value of the created Point
.
p.x = 42
is the assignment of the second ball.
p
method parameter is the second button (since passed by value/copy).
Don't forget: MUTATIONS (p.x = 42
for instance) are visible to the caller environment but not the case of REPLACEMENTS (assignment)
I hope you understand now why the output is 42 30
and not 30 30
as you surely expected ;)