• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JAVA Documentation Methods returns

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the java online documentation related to SE7 classes (like the class StringBuilder) you can notice that the methods may have 3 types of return :

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.

Thanks



 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
momo mom
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your clarification, that helped me a lot since i was not able to understand why 2 and 3 were different .

But someone can always ask you, why Oracle puts such confusing #3 return type in the above mentioned case ?
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

momo mom wrote:But someone can always ask you, why Oracle puts such confusing #3 return type in the above mentioned case ?


I have to admit it's not the best. When a method returns the object it's working on, I usually just put
Returns: this

However, the best programmers aren't always the best documenters (more's the pity).

Winston
reply
    Bookmark Topic Watch Topic
  • New Topic