This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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?
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.
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
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.