• Post Reply Bookmark Topic Watch Topic
  • New Topic

StringBuffer  RSS feed

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I am new bie to Java. When I am going through online I came across this method.But I didnt understand the Concept. Can any one help me in this

 
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

The method append() in the class StringBuffer takes the String argument and adds it to the contents of the StringBuffer. So if the StringBuffer contains "abc", and you call buffer.append("xyz"), then the buffer contains "abcxyz".

That much, you probably already knew.

Now, the designer of this method had an idea which s/he felt was clever. The "append" method doesn't need to return anything; it could just return 'void'. But instead, s/he made it return "this" -- i.e, it returns the object you call the method on. What good is this? Well, since buffer.append() returns the value of buffer, it can be substituted for "buffer" in most situations. Therefore, you can write

buffer.append("abc").append("xyz")

and this is equivalent to

buffer.append("abc");
buffer.append("xyz");

but it saves a little bit of typing. By the same logic, you can write

buffer.append("a").append("b").append("c").append("xyz");

Now, some people think this is cute, or even a good idea. Other people think it's a bad and ugly design. Other people are in the middle, and think this method chaining (as it's called) is OK in some limited circumstances, like this one.
 
Ram Adsumalli
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ernest

Thanks a lot for your description. I am unable to follow the below lines . Is there any chance that you can elobrate it.

Now, the designer of this method had an idea which s/he felt was clever. The "append" method doesn't need to return anything; it could just return 'void'. But instead, s/he made it return "this" -- i.e, it returns the object you call the method on. What good is this? Well, since buffer.append() returns the value of buffer, it can be substituted for "buffer" in most situations. Therefore, you can write

Thanks
Raghu
 
Ranch Hand
Posts: 259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is if the method signature of append in StringBuffer (refer Java doc if needed) was public void append(String str) instead of
public StringBuffer append(String str)

you may have to code
buffer.append("a").append("b").append("c").append("xyz"); as below.

buffer.append("a");
buffer.append("b");
buffer.append("c");
buffer.append("xyz");

[ January 09, 2007: Message edited by: karthi keyan ]
 
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ram Adsumalli:
... Is there any chance that you can elobrate it...

A StringBuffer's append method doesn't just append the StringBuffer, it also happens to return a reference to the appended StringBuffer.

For example, consider the line...

buffer.append("abc").append("xyz");

Here, buffer.append("abc") doesn't just append "abc" to the end of the StringBuffer, it also returns a reference to the appended StringBuffer (the same object that buffer is pointing to). Because this returns a reference, that reference can be used to call append again, this time appending "xyz".
 
Ram Adsumalli
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

When I am working in Eclipse with debug mode and I am debugging the code, then i am getting yellow line in variables Column in Eclipse.Is that yellow stands for any error message

Ram
 
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ram Adsumalli:
Hi,

When I am working in Eclipse with debug mode and I am debugging the code, then i am getting yellow line in variables Column in Eclipse.Is that yellow stands for any error message

Ram


Ram, please start a new thread for this question, preferably in our IDE forum. Thanks!
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to add to what already has been said, the last line in StringBuffer.append is

return this;

Perhaps that helps to understand what's going on.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!