Thursday, September 3, 2009

Why use StringBuilder instead of StringBuffer?

The String class is just not sufficient to perform all the String related tasks.That is why the classes like StringBuffer and StringBuilder is added.StringBuffer class is added in Java 5 .It has exactly the same API as StringBuffer except (the twist in the story) ,the methods in StringBuilder class are not Thread safe.We can also say that the methods in StringBuilder class are not synchronized.The recommendation of many experienced programmers and Sun itself is that use StringBuilder instead of StringBuffer because the StringBuilder will run faster than StringBuffer.

Apart from this huge difference between StringBuilder and StringBuffer, everything which is true for StringBuffer holds true for StringBuilder class.

NOTE:-If you want to give SCJP examination then you must clearly know the distinction between the String,StringBuffer and StringBuilder class.The questions may be on how well you know the API of these classes and implementation of these classes.So there may be questions on StringBuilder or StringBuffer which may contain some piece of Thread code.Don't be panic by watching the Thread code.Just Rehearse the use of StringBuilder and StringBuffer in Threads.It is not that tough.

Suggested Reading:-

How JVM handles Strings
Method Chaining in Java Strings
Important String Methods
Simple twisting questions on Strings
StringBuffer class explained

Sponsored Listings
Get your SCJP kit freee
Are you Intelligent!!!!!

0 comments:

About This Blog

This Blog is all about Java and programming.This blog is written by Vaibhav Pandey .He is a Computer Science Graduate .You can contact the author at javatute@gmail.com for any suggestion or Query.You can also contact the author for advertising on this blog.All the material presented here is the property of author and its reproduction in any form is strictly prohibited.

Disclaimer:-Download links provided here are not of author in any means.These are found over the Internet.
Page copy protected against web site content infringement by Copyscape

  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Back to TOP