0

This class ds has two fields, x and y

public class ds
{

private int x;
private int y;

public ds(int xx, int yy)
{
    x = xx;
    y = yy;
}

public int getX()
{
    return x;
}

public int getY()
{
    return y;
}

public void setX(int xx)
{
    x = xx;
}

public void setY(int yy)
{
    y = yy;
}

}

This class ptrs uses ds to print results. I note that the printouts are the same whether int or Integer is used in ds.

public class ptrs
{
public static void main(String[] args)
    {
    ds d = new ds(1,2);

    System.out.println("d.x:" + d.getX()); //1
    System.out.println("d.y:" + d.getY()); //2

    //t is an object reference (a pointer) which points to d, which in turn 
    points to the ds object
    ds t = d;

    System.out.println("t.x:" + t.getX()); //1
    System.out.println("t.y:" + t.getY()); //2

    t.setX(3);
    t.setY(4);

    System.out.println("");

    System.out.println("t.x:" + t.getX()); //3
    System.out.println("t.x:" + t.getY()); //4


    System.out.println("d.x:" + d.getX()); //3
    System.out.println("d.y:" + d.getY()); //4

    d.setX(5);
    d.setY(6);

    System.out.println("");

    System.out.println("d.x:" + d.getX()); //5
    System.out.println("d.x:" + d.getY()); //6

    System.out.println("t.x:" + t.getX());  //5
    System.out.println("t.x:" + t.getY());  //6
    }
}

When I call the set methods on either d or t, calling the get methods on either pointer results in the updated values. Why is there apparently different behavior in the next example?

public class main
    {
    public static void main(String[] args)
        {
        Integer i = new Integer(5);
        Integer a = i;

        System.out.println("i is " + i ); //5
        System.out.println("a is " + a ); //5

        i = new Integer(10);

        System.out.println("i is " + i ); //10 
        System.out.println("a is " + a ); //5
        }
    }

If I set an Integer object reference i to point to an Integer object with value 5, and then make another reference a refer to i, why does a still point to 5 even after I make i reference another Integer object with value 10?

Is the difference because in the first example I change fields of the same object, but in the second example I point to a new object? But why would that cause a difference, if that is the reason..?

Josh
  • 61
  • 6
  • I understand Java is pass-by-value. As I understand, a variable in a function A, when passed to a function B and is manipulated by function B locally, then afterward keeps its original value after B is returned from and execution continues in A. I thought these examples might be different because my concern in the first example doesn't deal with the values before calling the set methods, because I'm passing in constants, and in the second example I don't define another function besides main. If it is somehow similar, could you explain to me how? – Josh Jan 08 '19 at 13:21
  • Object variables basically "pointers" to an object someplace in memory. When you do `a = i`, you don't point `a` directly to `i`, but to the memory that `i` is pointing to. So even if `i` changes later, `a` would not see it because it points to the original object in memory, not to `i` itself. – TiiJ7 Jan 08 '19 at 13:29

3 Answers3

3

Because in the first example there is only 1 object. Even though you are changing the values of x and y, it is the same, single object.

In the second example there are 2 objects. I hope below image will clear it out.

image

Prasad Karunagoda
  • 2,048
  • 2
  • 12
  • 16
0

Is the difference because in the first example I change fields of the same object, but in the second example I point to a new object?

Yes

But why would that cause a difference, if that is the reason..?

Because it's useful that way?

kumesana
  • 2,495
  • 1
  • 9
  • 10
  • Can you explain in more detail? How did the developers go about making it useful this way? – Josh Jan 08 '19 at 13:17
  • It gives you the opportunity to sometimes have different objects, that aren't modified by modifications made on the other, when that's something helpful to you. And other times share an object to other parts of the code, which when they modify it, all other parts of the code account for this modification. – kumesana Jan 08 '19 at 13:19
  • I still want to understand how this works, say, at the address level. Would you give me an overview of addressing in Java? If `i` is a pointer which holds the address of the first Integer object, and `a` holds the address of `i`, then if the address contained in `i` changes, then I would think that the change should also be reflected when getting the object through the reference `a`. You haven't explained how or why what you have said works, works. – Josh Jan 08 '19 at 13:24
  • Hmm. I think you're confusing pointers with pointers of pointers. In your example, i and a are both of type Integer. They're the same type, not different. So they won't work differently. If one is a pointer to a final object, the other will also be a pointer to a final object, not a pointer to a pointer. When i is a pointer which holds the address of an Integer, and you assign i to a, then a is a pointer that holds the same address as i. You made it so a contains the same thing as i contains. – kumesana Jan 08 '19 at 13:28
  • Okay, that makes sense. – Josh Jan 08 '19 at 13:35
0

Your line:
i = new Integer(10);
assigns a whole new object to i, over-writing its previous value. The new object is completely independent of the object previously referenced by i and a. So i no longer refers to the object referenced by a - i and a then reference entirely different objects which (as you would expect) do not interact.

user-10884042
  • 31
  • 1
  • 5