aspose file tools*
The moose likes Java in General and the fly likes String and StringBuffer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "String and StringBuffer" Watch "String and StringBuffer" New topic
Author

String and StringBuffer

vipul bondugula
Ranch Hand

Joined: Oct 14, 2010
Posts: 218
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?


Thanks
Vipul Kumar
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7063
    
  16

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


Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
vipul bondugula
Ranch Hand

Joined: Oct 14, 2010
Posts: 218
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
Bartender

Joined: May 31, 2011
Posts: 1776
Probably you are asking for StringBuffer vs Builder in a multi threaded environment?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
vipul bondugula
Ranch Hand

Joined: Oct 14, 2010
Posts: 218
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

Joined: Mar 17, 2011
Posts: 7063
    
  16

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

Joined: Oct 13, 2005
Posts: 36501
    
  16
There is a similar discussion in this thread. You may have to scroll down to find it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String and StringBuffer
 
Similar Threads
Palindromes
string reversal problem
Propogating Exceptions - Exercise5.3 in K&B
String return form method
i dun understand....