aspose file tools*
The moose likes Java in General and the fly likes Reference passing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Reference passing" Watch "Reference passing" New topic

Reference passing

john j. farrell

Joined: Jun 22, 2003
Posts: 1
Can someone describe to me step by step how references passed in the following code result in the output:
15 0

public class Test
public static void main(String args[])
Test t = new Test();
public void first()
int i = 5;
Value v = new Value();

public void second(Value v, int i)
i = 0;
v.i =20;
Value val=new Value();
v= val;
System.out.println(v.i +" " + i);

class Value
public int i = 15;
Malcolm Featonby

Joined: Jun 18, 2003
Posts: 14
Sorry John - its quite a long winded response.
The key thing to remember is that java passes everything by value and
nothing by reference.

In this diagram (which I hope works on posting)each horizontal section represents a new set of values on the stack.
Objects are never passed to methods, only references to objects are passed to methods.
This is demonstrated in line 24 where the value held in "val", a reference to a new instance
of Value() is assigned to the reference variable "v". This results in "v" holding a
reference to the new instance of Value(). It does not have any impact on the
instance of Value() previously referenced by this local variable.
so :
- first() has an int called i with a value of 5 and an instance of Value() called v.
- second() has another int also called i which is a parameter variable and so is assigned the value passed on the call. This int's value is subsequently changed to be 0 on line 21. There is also a parameter variable which is a reference to a Value() instance. This variable holds a reference to the instance of Value() created on line 13. On line 22 we use this reference variable to change the value of i held in the instance of Value() to be 20. On line 24 we then change the value of the reference variable to refer to a new instance of Value().

It is important to distinguish between references to objects and the objects themselves.
Something that is well illustraded by your code example.
I hope this helps,
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Wow, that's a neat way to illustrate it. I try to remember that Java always makes a COPY of the variable being passed.
If you call a method with a primitive argument, say an int variable, Java makes a copy of the variable. If the method changes the int parameter (flagged as an error by some) it changes its own copy, so the original is not changed.
If you call a method with an object argument, say HashSet, Java makes a copy of the variable again. But this time the variable is a pointer to an object. If the method changes the pointer to a different Set, it only changes its own copy of the pointer and the original pointer is not changed. Exactly like the int.
What's different is that the copy of the pointer points to the same HashSet as the original pointer. If the method adds or removes elements in the Set, the original Set is modified. That's nothing like the int example, because the int was not a pointer.
At the beginning I said Java always makes a copy, which is what makes it "by value". Copy sounds expensive, but recall it only copies primitives like int, and pointers which are also just a few bytes. Not a big deal.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Darryl Failla
Ranch Hand

Joined: Oct 16, 2001
Posts: 129
I think you're confusing yourself quite a bit by using the same variable name too much. If you would change the second() method to:
public void second(Value x, int i)
i = 0;
x.i =20;
Value val=new Value();
x= val;
System.out.println(x.i +" " + i);
This preforms exactly the same operation, but I think the procedure would be a lot clearer to you.

Darryl Failla
Sun Certified Java 2 Programmer
I agree. Here's the link:
subject: Reference passing