Win a copy of React Cookbook: Recipes for Mastering the React Framework this week in the HTML Pages with CSS and JavaScript forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

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.
 
And tomorrow is the circus! We can go to the circus! I love the circus! We can take this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic