aspose file tools*
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
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: StringBuffer question