• Post Reply Bookmark Topic Watch Topic
  • New Topic

Object references/Primitives as method parameters  RSS feed

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is an excerpt from Marcus Green's Last Minute tutorial (section 5.4)
The answer is number 4. (10, 0, 20).
I do not understand why the last output is 20, I thought that it should of been 30.
Can someone please explain.
--------------------------------------------------
Given the following code what will be the output?
class ValHold{
public int i = 10;
}
public class ObParm{
public static void main(String argv[]){
ObParm o = new ObParm();
o.amethod();
}
public void amethod(){
int i = 99;
ValHold v = new ValHold();
v.i=30;
another(v,i);
System.out.println(v.i); //here
}//End of amethod
public void another(ValHold v, int i){
i=0;
v.i = 20;
ValHold vh = new ValHold(); //here
v = vh; // here
System.out.println(v.i+ " "+i);
}//End of another
}
1) 10,0, 30
2) 20,0,30
3) 20,99,30
4) 10,0,20
Answers
Answer 1)
4) 10,0,20
--------------------------------------------------
 
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because you set the value of i to 20 in another().
The object that the reference "v" in amethod() refers to is the same object that the reference "v" in another() refers to. So when you do v.i = 20;, it makes the change for all references to this object. However, when you do v=vh;, the copy of the reference now points to a whole new object, but the "v" in amethod() still refers to the other object.
Clear as mud?
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's important to remember that all paramaters in Java are passed by value. That means that you'll always get a copy of the variable as a parameter. That might be a copy of a primitive value or a copy of a reference to an object.
If you want more information about this, you should probably check out the Programmer Certification Forum. This topic is brought up almost weekly.
Due to the common discussion of this topic, I've created a small Flash application that might help you visualize what is taking place. Check it out here.
I hope that helps,
Corey
[ April 02, 2002: Message edited by: Corey McGlone ]
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Very nice Flash demo, Corey!
However, I think I would disagree with all parameters are passed by reference. I see it as all parameters are passed by value, the value either being a copy of a primitive value or the copy of a reference value. References point to objects, not primitive data, so if a parameter is a primitive data type, no reference is involved!
 
Otto Deckelman
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason and Corey, you both have helped alot, the flash page explains the concepts thoroughly. Once the reference to v has been changed to vh ( that is v=vh) is v.i still point to the old object (v) or does it point to vh.i, I guess I'm still a little confused on this.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by jason adam:
However, I think I would disagree with all parameters are passed by reference.

You're absolutely right. I've changed my post above to get rid of the typo. All parameters are passed by value in Java. I had the right explanation, but used the wrong term. That'll teach me to type too fast...
Thanks, Jason.
Corey
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Otto Deckelman:
Jason and Corey, you both have helped alot, the flash page explains the concepts thoroughly. Once the reference to v has been changed to vh ( that is v=vh) is v.i still point to the old object (v) or does it point to vh.i, I guess I'm still a little confused on this.

Okay, let's look at this closely.
Start in the method amethod.
We execeute the following code:

At this point, we've created a variable that holds an int primitive, i, and a reference variable that points at a new object of type ValHold. We also set the variable i, within that object, to 30. To draw a picture, we have something like this:

Next, we call the method another, and pass it i and v. Notice that we don't pass the object that v references, we pass a copy of v, the reference, itself.
Now, we execute the following code:

When we start this method, we have something like this:

Due to the fact that the variable in the method amethod and the parameter in the method another both have the name v, I've renamed (in the drawing) the parameter in the method another to param.v. It's really the same variable, I just need a way to keep them distinct.
After the first two lines of another have been executed, we have this:

Notice that the i in this method is in no way related to the i in the method amethod. Even after this i is set to 0, the other i remains at 99. Notice however, that we set v.i to 20. Since the v in the method another references the same object that the v in amethod refers to, that change will be visible from the reference variable in amethod.
Next, we execute these lines of code:

After that, our picture has changed a little:

Notice that our parameter now references a brand new object.
Now, when we print out the values v.i (that's param.v.i, for my drawing) and i, we use the ones from the picture: 10 and 0, respectively.
Now, execution goes back to amethod, where we execute another println:

This uses the object referenced by v, which contains the value 20. That's how you get the final output.
I hope this makes sense. If not, just ask more questions, we'll get it worked out eventually. This is a rather confusing topic to understand at first.
Corey
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Corey McGlone:

You're absolutely right. I've changed my post above to get rid of the typo. All parameters are passed by value in Java. I had the right explanation, but used the wrong term. That'll teach me to type too fast...
Thanks, Jason.
Corey

Ok, that explains why you later talk about nothing but values. Don't it stink having too much in your head to say and not enough hands/fingers to type it all out at the same time?
 
Otto Deckelman
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Corey, you have cleared it up for me, thanks abunch. You guys are great and it is very helpful to be able to ask someone a techie question especially when no one in my shop (office) is SCJP and are competent Java ethusiasts.
 
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have lots of confusion in pass by value(reference) in java. The Flash link provided by you for explaining it is not working. Whether it is possible to give that link


It's important to remember that all paramaters in Java are passed by value. That means that you'll always get a copy of the variable as a parameter. That might be a copy of a primitive value or a copy of a reference to an object.
If you want more information about this, you should probably check out the Programmer Certification Forum. This topic is brought up almost weekly.

Due to the common discussion of this topic, I've created a small Flash application that might help you visualize what is taking place. Check it out here.

I hope that helps,
Corey


//JAM -- Edited to change from CODE to QUOTE tag.
[ July 14, 2005: Message edited by: Joel McNary ]
 
Sheriff
Posts: 9082
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mark Jim Tony:
I have lots of confusion in pass by value(reference) in java. The Flash link provided by you for explaining it is not working.

Not too surprising since the link is 3 years old.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!