aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Pass a StringBuffer to a method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Pass a StringBuffer to a method" Watch "Pass a StringBuffer to a method" New topic
Author

Pass a StringBuffer to a method

James Inge
Greenhorn

Joined: Dec 04, 2000
Posts: 12
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

Joined: Oct 21, 2000
Posts: 4347
    
    2

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.


SCJP
Visit my download page
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
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


"JavaRanch, where the deer and the Certified play" - David O'Meara
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Dec 03, 2000
Posts: 19
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

Joined: Sep 23, 2000
Posts: 3324
    
    8
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


Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
James Inge
Greenhorn

Joined: Dec 04, 2000
Posts: 12
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

Joined: Jun 30, 2000
Posts: 1070
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Pass a StringBuffer to a method