• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Q from Marcus ex-2?

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
The ans is given as 4.How is it possible and why?Can anybody there clear my doubt?

Thanx in advance
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where is your class ValHold?
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when you pass an object reference as a parameter
the changes made to the "object" will affect the object (v.i)
but
the changes to "object reference" will not be affect it.(v will not change)
object is passed by reference
object reference is passed by value

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 after coming back from the method another v=vh does not hold good. v still points to the same old object.
But the object's i value was changed in "another" method so v.i=20 */
}//End of amethod
public void another(ValHold v, int i){
i=0;
v.i = 20; //this changes the value of i of the object that is passed
ValHold vh = new ValHold();
v = vh;// this does not change the value of v when the execution returns to amethod
System.out.println(v.i+ " "+i);
// v.i=10 as vh.i=10 ; this is true only in this method ;i=0 local variable
}//End of another
}
1) 10,0, 30
2) 20,0,30
3) 20,99,30
4) 10,0,20
So the answer is 4 .
[This message has been edited by Mary Anitha (edited October 24, 2000).]
[This message has been edited by Mary Anitha (edited October 24, 2000).]
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I studied the code...and as per my reading the answer should be 2>20,0,30....
please correct,if I am wrong...
Thanks
Amit
 
Mary Anitha
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Amit
I think u missed v=vh in the following method
when vh was created vh.i=10
when v is made equal to vh
v.i will have 10 but in this method only, not after returning from this method
In line 3 the value of i was changed which is reflected in the calling object.
1) public void another(ValHold v, int i){
2) i=0;
3) v.i = 20;
4) ValHold vh = new ValHold();
5) v = vh;
6) System.out.println(v.i+ " "+i);
7) }//End of another
 
Amit Tyagi
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Anitha,
Thanks... I really missed that important point....

Bye
Amit
 
This tiny ad will self destruct in five seconds.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic