1

I'm studying for an exam, and I'm looking through a sample program and I am confused. Here is the code:

public class Problem1 {
public static void f(A X)
{
    A Y = X;
    Y.key = X.key + 1;
}

public static void f(B X)
{
    B Y = new B();
    Y.key = X.key + 2; //Y.key = 12
    X = Y; //X points to Y? 
}

public static void main(String[] args)
{
    A P = new A();
    P.key = 3;
    B Q = new B();
    Q.key = 10;
    f(P);
    System.out.println(P.key);
    f(Q);
    System.out.println(Q.key);
    P = Q;
    f(P);
    System.out.println(P.key);
    }
    }

    class A
    {
public int key;
    }
    class B extends A 
    {

    }

I am fine with f(P). The question I have is with f(Q). I get that a new B named Y is made, and it's key is 12. The question I have is that, shouldn't X = Y point X to Y? Making Q's key value 12 rather than 10? The code prints out 4,10,11. I'm confused as to why it prints 10 rather than 12.

JerryCrowley
  • 145
  • 1
  • 12
  • 2
    Everything in Java is *pass by value* `X = Y;` is local to the method and so, it doesn't touch the Object in `main` since the local reference `X` now points to an entirely different Object, leaving you with `10` printed instead of `12`. – A--C Mar 13 '13 at 01:23
  • Then, why is P's key value changed from 3 to 4 in "public static void f(A X)"? – JerryCrowley Mar 13 '13 at 01:26
  • 1
    in `public static void f(A X)`, Y still points to the same Object (you never overwrite what X points to in that method, so when you do `Y.key = X.key +1`, Y references what X references, which is the Object created in `main`) so the changes are reflected. – A--C Mar 13 '13 at 01:28
  • If you really want to figure this out, rewrite the variable names, they're confusing and can lead you down the wrong path if you don't pay close attention. Make them challenging *after* you've grasped the concept. – A--C Mar 13 '13 at 01:31
  • I kind of get it. So X = Y is not the same thing as A Y = X? Aren't they both referencing to another variable? – JerryCrowley Mar 13 '13 at 01:37
  • It depends on the context, in `f(A X)` does different things than `f(B X)`. Look at [this (closed) question](http://stackoverflow.com/questions/15374745/why-doesnt-pass-by-refernce-work#comment21727501_15374745) for more details. The duplicate question and [Eng.Fouad's linked answer](http://stackoverflow.com/questions/9404625/java-pass-by-reference/9404727#9404727) are very helpful. Your question is also a duplicate IMO, but it's a bit more "original" – A--C Mar 13 '13 at 01:41

1 Answers1

3

In java each variable of a class type, like P, Q, X etc, is a reference to an object (or null). You can imagine that there's an object somewhere in memory and the variable points to it:
P -----> (P object)
When you call f(P), the first f method receives a reference to the same object, but it is a different reference:
(main) P -----> (P object)
(f) X -----> (P object)
Then f makes yet another reference:
(f) Y -----> (P object) And when it changes the key, it changes it in the same object.

In the second case, f(Q), the second f method again receives a (different) reference to the same object:
(main) Q -----> (Q object)
(f) X -----> (Q object)
then it makes a new object: (f) Y -----> (Y object)
then it changes the key in this object, and sets the X variable to point to this object:
(f) X -----> (Y object, key=12)
however, in your main method, the Q variable hasn't changed:
(main) Q -----> (Q object)
because X was just a copy of Q, it's not Q itself. So Q still points to the Q object, which was not modified, the Y object was modified.