momo mom wrote:1) Return new Object : public String substring(int start)
2) Retrun a refrence to the object : public StringBuilder insert(int offset, String str)
3) Return this object : public StringBuilder replace(int start, int end, String str)
My question is what are the differences and similarities between these 3 types of returns.
Well first of all, there are only two, since both 2 and 3 return
this. And if you're quoting the docs verbatim, #1 returns "The new string".
The fact is that most methods return
something; and in the case of StringBuilder, since its business is to build new Strings, and it's
mutable, the fact that most of them return the StringBuilder they just modified allows you to chain operations, viz:
System.out.println( new StringBuilder().append("World!").insert(0, "Hello ").toString() );
HIH
Winston