File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes StringBuffer class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "StringBuffer class" Watch "StringBuffer class" New topic
Author

StringBuffer class

Mansi Vyas
Greenhorn

Joined: Jul 01, 2003
Posts: 27
All of the StringBuffer methods operate on the value of the StringBuffer objects invoking the method. So why do some of the StringBuffer methods such as append, insert, reverse have StringBuffer as a return type? What is the point of this? The only reason I can think of is so that method calls can be chained to each other. Is this correct? Or am I missing something?
Thanks.
Mansi
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
That's right, having the method return a reference to the StringBuffer allows you to chain multiple invocations together, like this:


SCJP Tipline, etc.
Mansi Vyas
Greenhorn

Joined: Jul 01, 2003
Posts: 27
Are these methods just returning a reference to this (the StringBuffer object invoking the method)?
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Mansi Vyas:
Are these methods just returning a reference to this (the StringBuffer object invoking the method)?

Yup. In effect, they are performing the desired operation and then doing a "return this".
 
 
subject: StringBuffer class