Showing posts with label String builder. Show all posts
Showing posts with label String builder. Show all posts

Sunday, August 2, 2009

String concatenation and String builder confusions resolved


There was a very hot debate going on about String vs String builder and doing string concatenation within the append method of the String builder. I then researched this topic on the net because i had to prove a point in my work area ;) ... Then i stumbled upon this great blog post which i thought i should share with all of you which clearly explains when and how to use these String concatenation methodologies. And thankfully i was right in my assumptions :D.... The blog post can be found at http://www.znetdevelopment.com/blogs/2009/04/06/java-string-concatenation/

Friday, April 3, 2009

String builder/buffer performance

If you ever wanted to do string manipultion what you would turn to would be one of the following as opposed to using raw String objects which we know is not good for string manipulation due to the object construction on the heap. But the only issue with String builder/buffer is that even those it has append methods, it does not have a clear method which would clear out the buffer and enable the user to start fresh. There are two ways in which you can achieve this;
 
    StringBuilder str = new StringBuilder();
    str.delete(0,str.length);
 
    or
    str.setLength(0);
 
    From the above two methods it is said that performance vice it is always advisable to use the later method of setting the length to zero which will start appending strings from the zeroth position again rather than deleting the already existing strings within the buffer.