1
class Util{

   public static void F(Point p, Point q) {
      p.x = 42; 
      p = q;
   }

}

…
Point a = new Point(10,20);

Point b = new Point(30,40);

Util.F(a,b);

System.out.println(a.x + " " + b.x);

if someone can please explain me why the output is 42 30

thank in advance!

Mik378
  • 21,881
  • 15
  • 82
  • 180
user3136572
  • 134
  • 10

4 Answers4

1

when you do p = q that does not change the value of Point a, it just change the reference of the temporary variable p, to be q. but p.x=42 references to a value inside p, so that actually change it

Dima
  • 8,586
  • 4
  • 28
  • 57
1

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 ;)

Mik378
  • 21,881
  • 15
  • 82
  • 180
0

p and q are value copies of references. Within F, assigning p = q just changes what p is pointing to, not the actual object itself, or any value in the caller.

p.x = 42 changes the field x to have the value 42 in the object referenced by p.

Putting it all together, a will have its x field changed by Util.F, but b is not changed in any way.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
0

Method F changes value of field x in object passed as 1st argument to 42, while it doesn't change any state of fields in object passed as 2nd argument.

So, what you have done: you create object a, it doesn't matter what values you have passed inside its constructor: method F changed a.x to 42 <- this is the first part of your output.

Also you have created object b with x state equals to 30. Method F didn't change its values, so this is the second part of your output.

What about string code line p = q - it doesn't do anything worth. It just changes reference for variable p to be reference to object on which references variable q. It doesn't change object's state itself.

Andremoniy
  • 34,031
  • 20
  • 135
  • 241