• 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

Need help in string topic

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the below code, line 7 has

// sb1.append("def").reverse().insert(3, "---");

Here append() and reverse() are StringBuffer class methods whereas insert() is StringBuilder class method, we are chaining StringBuilder method with StringBuffer methods. I'm struck with this line. I hope this question is very basic but please clarify me with the line




Thank You
 
Ranch Hand
Posts: 529
19
Eclipse IDE MySQL Database Tomcat Server Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you created sb with value "abc" then sb.append("def") means appending "def" to "abc" results sb = "abcdef"

Now you created sb1 with value "abc" then sb1.append("def").reverse().insert(3, "---"); where first sb1.append("def") gives "abcdef" then .reverse("abcdef") gives "fedcba" then.insert(3, "---"); means insert "---" String at offset 3 so in "fedcba" at 3 is c so it inserts "---" there and results sb1 = "fed---cba"
Output:
sb>>abcdef
sb1>>>fed---cba
 
kumar dayalan
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes i can understand how the strings are getting manipulated here in this example. But my question is how they are chaining an StringBuilder method "insert()" with StringBuffer "reverse()". And apologies if i have misread or misunderstood the methods in the string class!!!
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kumar dayalan wrote:Yes i can understand how the strings are getting manipulated here in this example. But my question is how they are chaining an StringBuilder method "insert()" with StringBuffer "reverse()". And apologies if i have misread or misunderstood the methods in the string class!!!



Not sure how you got the StringBuilder part. Those methods exists for both StringBuffer and StringBuilder classes. And since you instantiated a StringBuffer to start with, there are no StringBuilders (and hence no StringBuilder method calls) in the code.

Henry
 
Ganish Patil
Ranch Hand
Posts: 529
19
Eclipse IDE MySQL Database Tomcat Server Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kumar dayalan wrote: how they are chaining an StringBuilder method "insert()" with StringBuffer "reverse()"

It is method of StringBuffer because here you have created an object of StringBuffer not StringBuilder. Yes StringBuilder also has method with same name and arguments but the method you used here is of StringBuffer not StringBuilder.
Here is code gets called when you use .insert()
 
kumar dayalan
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You Henry and Thank You Ganish. Now i understood.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Eclipse IDE MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your welcome
 
reply
    Bookmark Topic Watch Topic
  • New Topic