Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "StringBuffer question" Watch "StringBuffer question" New topic

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
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.

Serena Zhou
Ranch Hand

Joined: Dec 13, 2003
Posts: 31
Yeah, Thanks!
String is immutable and StringBuffer is not.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: StringBuffer question
jQuery in Action, 3rd edition