This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv 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 REST with Spring (video course) this week in the Spring forum!
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 agree. Here's the link:
subject: StringBuffer question
It's not a secret anymore!