File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

StringBuffer [why this return?] append([implicit this])

 
Guennadiy VANIN
Ranch Hand
Posts: 898
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why mutable StringBuffer instance in its methods, altering the same this object (like append(), insert(), etc.), have StringBuffer return(ed)?
All instance methods already have implicit reference to "this" instance
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just so you can say
sb.append("blah").append("foo").append("bar");
Which is arguably more readable than
sb.append("blah");
sb.append("foo");
sb.append("bar");
And a good deal more efficient than
sb.append("blah" + "foo" + "bar");
Because the latter needlessly creates a temporary StringBuffer and String. It is amusing (or saddening) how often you see developers use the latter idiom.
- Peter
 
Guennadiy VANIN
Ranch Hand
Posts: 898
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That was fast. Thanks. I understood that return is a copy of reference to StringBuffer instance
 
Guennadiy VANIN
Ranch Hand
Posts: 898
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Peter.
cool! I asked before posting and after posting here a lot of programmers/developers in Java. Nobody could figure out
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I still don't understand the original question. (But I think I understand the answer.)
 
Guennadiy VANIN
Ranch Hand
Posts: 898
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cristopher,
why, having understood answer, you could not understand qs.
Credits to Peter den Haan!!!
Anyway it is even more important (than receiving answer to specific technical qs) to be clear, and to be understood.
The questions was:
Why StringBuffer's methods, changing the same obj (not same as in case of String's methods), have returns of type StringBuffer but not of type void or boolean? I even could give counter-examples for similar circumstances...
Give me a word what should I correct more.
[This message has been edited by G Vanin (edited October 24, 2001).]
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic