Memory Diagram Example



Here is a sample program involving the Point class:

public class TestProgram {
    public static void main(String[ ] args) {
          Point a;
          Point b;
          a = new Point("first",0,0);
          b = a;
          a = setX(4);
          b = setY(5);
          a = new Point("second",-3,-8);
          Point temp = a;
          b = temp;
          temp = null;
          a.setY(b.getY());
          a = temp;
    }
}


It would be instructive to ask, "What does the memory diagram look like at the end of the computation?" and then answer it by making a memory diagram and updating it at every step of computation.

Below is a table contatining the code from above in the first column, with informative print statements about the object reference variables used in the program, wherever the call to the toString() method would not result in a NullPointerException. The next column explains what computation is occurring, while the last two columns show the current memory diagram and the resulting print statements, respectively.

For all of the computation below, remember:
  • that assignment statements associate from right to left
  • the value of an object reference is the object itself to which the reference points, and NOT the data member values of that object




  • a test program what's happening object diagram print out
    Point a; a Point object reference variable named
    "a" is declared
    Point b; a Point object reference variable named
    "b" is declared
    a = new Point("first",0,0);

    System.out.println(a);
    an instance of the Point class is created
    with the values "first", 0, and 0. Then,
    object reference variable a is assigned
    to the object.
    Point first is located at (0,0).
    b = a;

    System.out.println(a);
    System.out.println(b);
    object reference variable b is assigned
    the value of object referece variable a,
    which is the object to which it points.
    Point first is located at (0,0).
    Point first is located at (0,0).
    a.setX(4);

    System.out.println(a);
    System.out.println(b);
    setX(), with argument 4, is called on the
    object to which object reference variable
    a points.
    Point first is located at(4,0).
    Point first is located at(4,0).
    b.setY(5);

    System.out.println(a);
    System.out.println(b);
    setY(), with argument 5, is called on the
    object to which object reference variable
    b points.+
    Point first is located at(4,5).
    Point first is located at(4,5).
    a = new Point("second",-3,-8);

    System.out.println(a);
    System.out.println(b);
    an instance of the Point class is created
    with the values "second", -3, and -8. Then,
    object reference variable a is assigned to
    the object.
    Point second is located at(-3,-8).
    Point first is located at(4,5).
    Point temp = a;

    System.out.println(a);
    System.out.println(b);
    System.out.println(temp);
    a Point object reference variable named
    "temp" is declared and assigned the value
    of object reference variable a;
    Point second is located at(-3,-8).
    Point first is located at(4,5).
    Point second is located at(-3,-8).
    a = b;

    System.out.println(a);
    System.out.println(b);
    System.out.println(temp);
    object reference a is assigned the
    value of object reference b.
    Point first is located at(4,5).
    Point first is located at(4,5).
    Point second is located at(-3,-8).
    b = temp;

    System.out.println(a);
    System.out.println(b);
    System.out.println(temp);
    object reference b is assigned the
    value of object reference temp.
    Point first is located at(4,5).
    Point second is located at(-3,-8).
    Point second is located at(-3,-8).
    temp = null;

    System.out.println(a);
    System.out.println(b);
    the object reference temp is assigned
    the value of null.
    Point first is located at(4,5).
    Point second is located at(-3,-8).
    a.setY(b.getY());

    System.out.println(a);
    System.out.println(b);
    setY(), with argument of getY()
    called on the object that object
    reference variable b points to, is
    called on the object that object
    reference variable a points to.
    Point first is located at(4,-8).
    Point second is located at(-3,-8).
    a = temp;

    System.out.println(b);
    object reference variable a is
    assigned the value of object
    reference variable temp.
    Point second is located at(-3,-8).
    System.out.println(b); since there is an object to which
    no object reference variables point,
    that object will be deallocated
    during garbage collection, and no
    more object reference variable may
    point to it.
    Point second is located at(-3,-8).