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

Jai :6 on passing by reference

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can somebody help me understanding the following outcome!!
&&&
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:
d. a is One more
b is Two
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jaideep Mahajan,
Please note that JavaRanch has very few rules but the one that we strictly enforce is our naming requirement. You agreed to it when you registered so please follow it. Your display name must be your rell first name + space + real last name. "Jaideep Mahajan" would be perfect. Thanks.
As to your question:
b=a does nothing as all your doing is swapping local pointers. (local to the swap method.)

If I change the local variable names to x and y does that make it a little clearer?
 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But why a is changed while b still keep the same value? Could you please give some more details about this? Thanks !!


As to your question:
b=a does nothing as all your doing is swapping local pointers. (local to the swap method.)


 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As a matter of interest, what Java books are you reading?
BTW the "Naming Policy" Gru will keep sapping your energy until his wishes are satisfied. Otherwise you will be doomed to wander in complete ignorance...
Look! He knows your name already, beware!
[ August 13, 2002: Message edited by: Barry Gaunt ]
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Let's take ur code in some depth u 've two StringBuffer objects. n there handeles r a and b in main method(I will call them amain and bmain).
U passed the references(handeles) of these objects to a method sweep .
Method Sweep has its own references but towards those Objects creadted in main method.
now when u change the object referred by a(of sweep) it actually changes the Actual object.
but when u excute a=b; u actually shifts the reference of bsweep from the object in main.
That's why "b is Two " printed.
If u 've any more query abt. this u can mail me personnaly at inampaki@lycos.com
Regards
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jai007,
Please change your name to be compliant with JavaRanch's naming policy.
Your displayed name should be 2 separate names with more than 1 letter each. We really would prefer that you use your REAL name.
You can change your name: here.
Thanks,
Cindy
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic