• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

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 [email protected]
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
 
I carry this gun in case a vending machine doesn't give me my fritos. This gun and this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic