• 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

String and StringBuffer

 
Ranch Hand
Posts: 253
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Every one,

I hava a reverse string program which i coded by using both String and StringBuffer.

I am posting it ..



one way is



the other way is



In both ways iam getting correct output..
My doubt is in the last code..
I am concatenating each char to string.This means each time i add a character a new string is created.I am guessing it..Is it right?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

vipul bondugula wrote:I am concatenating each char to string.This means each time i add a character a new string is created.I am guessing it..Is it right?


Yes.

Winston
 
vipul John
Ranch Hand
Posts: 253
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Winston,

I gussed the response.Any way once again thank you.

Which one among String and StringBuffer is better to use? Explain in single user and multi-user applications..

I mean single thread and multi-thread applications..

Thanks & Regards
Vipul Kumar.
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Probably you are asking for StringBuffer vs Builder in a multi threaded environment?
 
Sheriff
Posts: 22787
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The number of threads doesn't matter. Strings are immutable, and the StringBuilder (which should be preferred over StringBuffer unless you really need synchronization) is a local variable and therefore available for only the current thread.

What you should take into account is performance. The String concatenation will create quite a lot of new objects, whereas the StringBuilder / StringBuffer solution will only create the StringBuilder / StringBuffer and the resulting reversed String. That's definitely better.

I have one question about your logic though - why not allow an empty String? An empty String reversed is the same empty String. Instead of throwing an exception, simply return s itself.
 
vipul John
Ranch Hand
Posts: 253
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Rob,

I got a better answer.Actually i am working on custom exception handling. This is an example in kathy sierra scjp book. after completion on working on this example i got the doubt about String and StringBuffer


Thanks once again for everyone..

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

vipul bondugula wrote:This is an example in kathy sierra scjp book. after completion on working on this example i got the doubt about String and StringBuffer


Just a heads-up for you: Most of the sample code in the scjp book is written to help you to pass the exam, and it's very good for that. It should not, however, be taken as an example of how to do stuff; in fact some of it is downright awful.

Winston
 
Marshal
Posts: 79468
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a similar discussion in this thread. You may have to scroll down to find it.
 
reply
    Bookmark Topic Watch Topic
  • New Topic