Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Marcus Green Exam

 
Vineet Sharma
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
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);
}//End of amethod
public void another(ValHold v, int i){
i=0;
v.i = 20;
ValHold vh = new ValHold();
v = vh;
System.out.println(v.i+ " "+i);
}//End of another
}
This Question has been asked by other people but I still don't get why the answer should be 10, 0, 20.
Thanks a lot
Vineet
 
Rob Acraman
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vineet,
I assume you're OK with the 10,0 - but still stuck about the final 20 - Right?
First off, consider the following:

So, what will the output be - 123 or 456? Answer: 123.
Why? Because Java ALWAYS passes arguments "By Value". In other words, every time you pass an argument to a method, Java *COPIES* that argument. So, the called method then always operates on the *COPY*, leaving the original in the calling routine unchanged.
With that in mind, let's look at the exam code:

OK, this is the same as my "method1" above. A 'ValHold' object is created, its 'i' value set to 30, and then the 'v' reference to it is passed to 'another'.

When 'another' is entered, remember that argument values are copied. Therefore, there are now 2 *TOTALLY SEPARATE* variables (amethod's 'v' and another's 'v') pointing to the same object in memory.
'another' first sets that object's 'i' value to 20.
It then sets v to vh. However, this is 'another's 'v' variable - 'amethod's 'v' variable is UNCHANGED, and still pointing to the original object (with its 'i' still set to 20).
So, when 'another' returns, 'amethod's 'v' is still pointing to its original object, and its 'i' value is still 20.
Hope this helps
 
Apoon
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rob,
I won't agree with you, becoz in java when you send an argument, if it is a primitive type we are sending a value but if you send an object we are sending a reference. So if you modify the object value in the calling function I would expect it to change.
But your description just gives me confusion, here it is
When 'another' is entered, remember that argument values are copied. Therefore, there are now 2 *TOTALLY SEPARATE* variables (amethod's 'v' and another's 'v') pointing to the same object in memory.
'another' first sets that object's 'i' value to 20.
It then sets v to vh. However, this is 'another's 'v' variable - 'amethod's 'v' variable is UNCHANGED, and still pointing to the original object (with its 'i' still set to 20).
So, when 'another' returns, 'amethod's 'v' is still pointing to its original object, and its 'i' value is still 20.
Can you explain it clearly rob!!!
Thanks,
Apooon
 
Harry Chou
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Rob is right.
when passing to another method the v is actually a new reference that point to the same object, but when
ValHold vh = new ValHold();
v = vh;
this has forced the second v to point to other stuff.
so, second v has lost it's connection to the object referred by first v.
Harry
 
Rob Acraman
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Apoon,
Sorry for the confusion. I was trying to keep it simple - Honest !
OK, let's take the two points you raised:

I won't agree with you, becoz in java when you send an argument, if it is a primitive type we are sending a value but if you send an object we are sending a reference. So if you modify the object value in the calling function I would expect it to change.

Absolutely correct - I totally agree with you

Can you explain it clearly rob!!!

A large part of the problem I had in my original explanation was because the original code had the same variable name ("v") used in both "amethod" and "another". Let's re-write the code to make things clearer :

Now, let's follow the code through .....
a) "caller" creates a ValHold object at memory address 1234 (say). So, "v" is set to 1234.
b) do_set is called, passing in v. This results in variable local_copy being CREATED (along with all do_set's other variables like local_var) and initialised to v's value (1234).
The really important point is that v and local_copy do NOT occupy the same memory-space (although they both point to the same object at this stage) ; They are separate, distinct variables.
c) The "i" member of the ValHold object is set to 20.
d) local_copy is assigned to point to a new object. As mentioned in (b) above, this does not affect v at all.
e) do_set returns. v is still unchanged, pointing to the original ValHold object (at address 1234) whose i member is still 20 from step (c).

So, did I do better this time?
I hope it's clearer - let me know if not.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic