• 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

Passing Values

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator




Out put is 20 10 0 20 .

Its about passing values.Could someone help me out with a brief explanation

regarding the output please.

Thanks
sid
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Lets see the program code line by line.

1. class ValHold {
2. public int i = 10;
3. }
4.
5. public class Obparam {
6. public static void main (String args[]) {
7. Obparam o = new Obparam();
8. o.amethod();
9. }
10.
11. public void amethod ( ){
12. int i=0;
13. ValHold v = new ValHold();
14. v.i=345;
15. another(v,i);
16. System.out.println(v.i);
17. }
18.
19. public void another(ValHold v, int i){
20. i=0;
21. v.i=20;
22. System.out.println(v.i);
23. ValHold vh = new ValHold();
24. v = vh;
25. System.out.println(v.i);
26. System.out.println(i);
27. }//End of another
28. }

At line number 15 the ValHold object V is passed inside another() method. As obvious it will pass the referrence of the V object (as V is not a primitive data type). So if we change the state of the V (value of the instance variables of V object) inside another() method, it will reflect the V object. Because inside another() method it is using the same refference of V.

In line 21 we are changing the value of v.i (instance variable of V) to 20. So in line 22 it will print 20.

In line 23 we are creating another new ValHold object vh. By default the instance vairiable i of vh object will be initialized with 10 (ref. line no 2). In line number 24 we are copping the referrence of vh object into the object refference variable v. So after line number 24 v and vh both reference variables are refferencing to the same Object (object created at line number 23).
So in line 25 it will print 10 (The value of i of vh object)

At line 26 it prints the value of i, which is the parameter of the another()method. The value of i is passed from amethod() function (line number 15). The actual value of i in amethod() is 0 (line number 12). So inside another() method at line 26 it will print 0.

After completing another() method, the control will return back to amethod(). And it will print the v.i at line 16. At line 16 it will print 20, the value of v.i, which has been changed inside another() method (Line 21).
 
Sid Robin
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That was awesome explanation .Thanks man
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When the value (which is the reference to the object in this case) is passed that reference is copied. Initially, it changes the object that's being referenced (changes it's value to 20 - remember that the caller's reference variable is different that the method's 'copy' reference but they point to the same object) However, once the 'copy' of the reference variable points to a different object, it is no longer affecting the original reference in the calling method...so it prints 10 while the calling code finally prints 20. The last poster made a much clearer walk through, but maybe my perspective will add to that too
 
Sid Robin
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But how is it that a copy of a reference will be able to point to a different object while the original variable sticks to the previous object acting as a reference ?
 
Sid Robin
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Like in the above case sName(original) is pointing to "vandeleur" and sName(copy of sName) is also pointing to "vandeleur wiggy" . My question is how does the compiler distinguish from two sName's ?
 
After some pecan pie, you might want to cleanse your palatte with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic