• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

passing objects to a method

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
when you pass an objectreference as an arg to a method,the changes will be reflected back as far as I know..so here when you pass the two references a&b the output I am getting as onemore followed by two....in the method swap b is refering to a so b value is onemore in the swap method but in the main method it is giving me the output as onemore followed by two....can anyone please explain ..I will be so thankful to you..
public class Test {
public static void main(String args[]) {
StringBuffer a = new StringBuffer("One");
StringBuffer b = new StringBuffer("Two");
Test.swap(a,b);
System.out.println("a is "+ a +"\nb is " + b);
}
static void swap (StringBuffer a, StringBuffer b) {
a.append(" more");
b=a;
}
}
What will be the output?
Answer:
a. a is One
b is Two
b. a is One
b is One
c. a is One more
b is One more
d. a is One more
b is Two
e. a is One more
b is Two more
Regards
Krishna
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Krishna,
In the swap method you are making a change to the object which is referred to by reference "a" local to the method. Hence now the object holds "one more". Now, remember that the references a & b within the swap method are different from those in the main method. Now, reference b in main() & in swap() point to the same object until the statement b=a in the swap method. Which means that the reference b which is local to the swap method now points to object referenced by a. At the same time the reference b in main() is still pointing to the object holding "two". Hence the output will be "one more" followed by "two".
Hope this helps
Regards
Satya
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Krishna,
The answer must be "onemore" followed by "Two", becasue in swap method u r changing the actual contents of the object a, by appending "one", so it will be reflected in main object and the value of "a" would be "onemore", then u r assigning the a reference to b, in swap method the b would be pointing to the same object as "a" and the b would contain "onemore" in swap method, but in main , the b contents will remain un changed.
Actually we R changing the reference of object copy of b in the swap method only, so the changes will not be reflected in main method.
Anand
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Thank you for your detailed explanation.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic