Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Pass a StringBuffer to a method

 
James Inge
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • 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;
}
}
 
Randall Twede
Ranch Hand
Posts: 4467
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • 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.
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • 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
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • 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;
}
------------------
 
Yamuna Pattathil
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • 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).]
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3760
10
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
bill bozeman
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic