Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String and StringBuffer

 
vipul bondugula
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
  • 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?
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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 bondugula
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
  • 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.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably you are asking for StringBuffer vs Builder in a multi threaded environment?
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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 bondugula
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
  • 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
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
 
Campbell Ritchie
Sheriff
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a similar discussion in this thread. You may have to scroll down to find it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic