This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Cloud Application Architecture Patterns: Designing, Building, and Modernizing for the Cloud and have Kyle Brown, Bobby Woolf and Joseph Yodor on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

A funny Mock Question! Please help me to solve.

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got a mock question as following, the result should be 10, 0, 20. But I solve it with the result 10, 0, 30. the problem is why v.i become to 20 at last? How is the procedure make the v.i become into 20?
Thanks every one
Menghan
==========================================================
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);
}//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
}
1) 10,0, 30
2) 20,0,30
3) 20,99,30
4) 10,0,20
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider:
public void another(ValHold v, int i){
// ! Copy of the passed value i is changed to 0. Method amethod()'s i still the same.
i=0;
// ! Object variable i refered to by both another()'s v and amethod()'s v is changed.
// ! Remember a reference to the object v got passed into another().
v.i = 20;
// ! Now create a new object vh
ValHold vh = new ValHold();
// ! Point another()'s v to the new object. But amethod's v still points to its the original object.
v = vh;
System.out.println(v.i+ " "+i);
}//End of another
When another() returns to amethod(), amethod's v still points to the original object whose i value was changed to 20 in another(), to thats what prints.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Daryl,
============================================================
ValHold vh = new ValHold();
// ! Point another()'s v to the new object. But amethod's v still points to its the original object.
v = vh;
============================================================
I agree with you but
I'm still a little confused about one thing, the statement
set v to point to vh, the newly created instance; are you saying
that v as a reference is passed-by-value therefore whatever you
do inside the method another() will not affect the original v in
amothod()? Please clarify! Thanks!
chengx
 
daryl olson
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The call in amethod():
another(v,i);
passes a copy of the reference to the object (not the object) and a copy of the int value of i (which I believe you know).
In another(), a copy of this refernce now points at the original object.
amethod's v -----> |object of type ValHold|
another's v ----------^
so that the line in another:
v.i = 20;
changes the value i in the original object. Then:
ValHold vh = new ValHold();
v = vh;
creates a new object of type ValHold and sets the copied reference to point to it. The variable v in amethod() still points to the original object. So now we have:
amethod's v -----> |object of type ValHold|
another's vh ----> |another object of type ValHold|
another's v -----------^
Does this make sense?
reply
    Bookmark Topic Watch Topic
  • New Topic