This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes StringBuffer question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "StringBuffer question" Watch "StringBuffer question" New topic
Author

StringBuffer question

Serena Zhou
Ranch Hand

Joined: Dec 13, 2003
Posts: 31
Could anyone explain why this code from Dan Chisholm mock exam have the output of "ABABCABC"?

When s1 was passed to m1, shouldn't another copy of s1 be created and changed which does not affect s1 itself?
Nathaniel Stoddard
Ranch Hand

Joined: May 29, 2003
Posts: 1258
Serena,
Java does always pass by value, making a copy and then passing it. But also remember that in Java, references to Java is what is being copied. So, the reference is copied and pass. The copy still refers to the same StringBuffer though, so both m1 and m2 alter the same StringBuffer.


Nathaniel Stodard<br />SCJP, SCJD, SCWCD, SCBCD, SCDJWS, ICAD, ICSD, ICED
Serena Zhou
Ranch Hand

Joined: Dec 13, 2003
Posts: 31
Yeah, Thanks!
String is immutable and StringBuffer is not.
 
 
subject: StringBuffer question
 
Similar Threads
Stringbuffers
Please explain me the output in detail
is it enough
String buffer
What is the sequence of execution?