wood burning stoves 2.0*
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
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".
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: StringBuffer class
 
Similar Threads
Can we replace StringBuilder where we used StringBuffer in JDK1.5
Syncronizing variables or classes - Exercise 9-2
Exam Lab question on Thread
when a String is mutable
Strange behaviour ...., attention pls