aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes StringBuffer / StringBuilder methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "StringBuffer / StringBuilder methods" Watch "StringBuffer / StringBuilder methods" New topic
Author

StringBuffer / StringBuilder methods

Tiju Thomas
Greenhorn

Joined: May 25, 2008
Posts: 22
Why do StringBuffer / StringBuilder methods return a new StringBuffer / StringBuilder object? Is it not enough for the methods to do manipulation on the same object?

In case of String, it is ok as we will not be able to modify the same object. But why in StringBuffer / StringBuilder? Is there any practical use?
Sid Murrey
Ranch Hand

Joined: Jul 07, 2008
Posts: 58
It does not return a new object. If you read the Java6 API documentation, you see that the methods return "this object" or "a reference to this object".
Thus,
<blockquote>code:
<pre name="code" class="core">StringBuffer sb2 = sb.append("blah");</pre>
</blockquote>
does not create a new object, but rather both reference variables 'sb' and 'sb2' point to the same StringBuffer object on the heap.
Tiju Thomas
Greenhorn

Joined: May 25, 2008
Posts: 22
Thanks.

But why should it be returned at all? We will be anyway invoking the dot operator on a specific StringBuffer / StringBuilder object from where the call is made. Is there any meaning in getting a new reference variable referring to the same object there?
Sid Murrey
Ranch Hand

Joined: Jul 07, 2008
Posts: 58
Tiju, I don't really know for sure, but my guess would be that it is just for convenience.

Compare:
<blockquote>code:
<pre name="code" class="core">
public StringBuffer doStuff(StringBuffer sb) {
// do stuff
sb.append("blah");
return sb;
}
</pre>
</blockquote>

<blockquote>code:
<pre name="code" class="core">
public StringBuffer doStuff(StringBuffer sb) {
// do stuff
return sb.append("blah");
}
</pre>
</blockquote>
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
It's also useful if you want to chain method calls:

<blockquote>code:
<pre name="code" class="core">
String s = new StringBuilder()
.append("this")
.append("that")
.append("the other")
.reverse()
.toString();
</pre>
</blockquote>


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Pankaj Mittal
Ranch Hand

Joined: Jul 09, 2008
Posts: 32
Hi

There are insert and delete methods in StringBuffer and StringBuilder classes through which you can modify these objects and as all other said one can also append string there.


2008 SCJP(5.0) 88%
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: StringBuffer / StringBuilder methods
 
Similar Threads
doubt in thread
Diiference between String ,StringBuffer & StringBuilder
Can we delete a String Object from JVM String Pool?
StringBuffer & StringBuilder and equals()
Thread safety difference between StringBuffer and StringBuilder