• 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
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Pass a StringBuffer to a method

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I ran this code below and expected the output to be...
blah yar
blah yar
But I got...
blah
blah yar
I was under the impression that objects were pass by reference and therefore the assignment of b to a would set a's reference to that of b's, thus the output of the two lines would be the same. Can someone shed some light on this for me?
Cheers, James
public class bufTest{
public static void main(String Args[]){
StringBuffer temp = new StringBuffer("blah");
StringBuffer temp1 = new StringBuffer("blah");
rep(temp, temp1);
System.out.println(temp +"\n" + temp1);
}
public static void rep(StringBuffer a, StringBuffer b){
b.append(" yar");
a = b;
}
}
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you can change the value of the object the reference is pointing at however you cannot which object it points at. you pass a copy of the reference. hope that helps.
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In other words:
You passed a copy of the reference to temp in as a
and a copy of the reference to temp1 in as b
you used the copy of the reference to temp1 to directly change the object by saying b.append(" yar");
you then set the copy of the reference to temp to be the same as the reference to temp1 when you said a=b. You did NOT change the original reference to temp which still points at "blah".
Notice that if you add a line after the a=b which says a.append(" yar"); the print out would be
blah
blah yar yar
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
replace the code with this boss !!! u ll get what u want!!!
public static void rep(StringBuffer a, StringBuffer b){
a.append(" yar");
b.append(" yar");
a = b;
}
------------------
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In still other words...
1)rep(temp,temp1)
temp -------"blah"-------a
temp1-------"blah"-------b
2) b.append(" yar") // changes the object b points to.

temp -------"blah"-------a
temp1-------"blah yar"---------b
3)a=b // Now a also points to the same object as b.

temp -------"blah"
a-------b-----"blah yar" --------temp1
[This message has been edited by Yamuna Pattathil (edited December 05, 2000).]
[This message has been edited by Yamuna Pattathil (edited December 05, 2000).]
 
Enthuware Software Support
Posts: 4575
45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A similar frequently asked question is explained here...
http://enthuware.com/jqplus/FAJQuestions.html#PARAMETERPASSING
-Paul.

------------------
Get Certified, Guaranteed!
(Now Revised for the new Pattern)
www.enthuware.com/jqplus
 
James Inge
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your help. It has answered my question. I posted this just before I left for work last night, and it was annoying the hell out of me. Then I was brushing my teeth this morning and it came to me, a copy of the reference. Your assistance has eased my mind.
Cheers, James
 
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
suryateja,
We have a naming policy here at the Ranch and your name doesn't comply. Check out www.javaranch.com/name.jsp for more info.
Please re-register with a more appropriate name.
Thanks,
Bill
 
Yeah, but how did the squirrel get in there? Was it because of the tiny ad?
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic