Thursday, September 3, 2009

StringBuffer class in Java

StringBuffer is the class included in java.lang and is mostly used when we have to make a lot of modifications to Strings of characters.As String class does not support string mutability that is when we use Java Strings class our strings becomes Immutable and can never be changed.It has a crucial impact on the memory of the program.If we use String class we are left with numerous dangling strings in the memory which not only waste the memory but could also cause the memory overflow at some stage.Objects of StringBuffer class can be modified a number of times and even you don't need a reference to the object to assign.Simply use the method on the object and change will reflect on the object.StringBuffer class modifies strings over and over again without leaving behind a great effluence of discarded String objects.

The main use of StringBuffer class is in File I/O where large stream of data keeps on coming with constantly changing values.Thus making StringBuffer class use handy.Here the loss of memory never happens.In these cases, large block of characters are handled as units ,and StringBuffer objects are the ideal way to handle a block of data,pass it on and then reuse the memory block to handle the next stream of data.

Constructors:-

StringBuffer() //Creates a StringBuffer with initial capacity of 16 characters.

StringBuffer(String s) //Creates StringBuffer which contains same no of characters as in the String argument.

StringBuffer(int length) //Creates StringBuffer with specified number of characters.

lets see an example on StringBuffer

// don't forget to import the StringBuffer class(java.lang.StringBuffer.*;)
StringBuffer x = new StringBuffer("ABCDE");
x.append("FGH");
System.out.println(x);

Output would be :- ABCDEFGH

The main feature of StringBuffer methods are as follows :-

The main feature of this StringBuffer class is that all methods are thread safe that is marked as synchronized.With synchronized marking StringBuffer comes with a disadvantage that it works slower that every String API.To overcome the problem of this Thread safe and slow execution we use StringBuilder class.More on StringBuilder Later.

Suggested Reading:-

How JVM handles Java Strings
Method Chaining in Java Strings
Important String Class Methods
Simple twisting questions on Strings
Why use StringBuilder instead of StringBuffer?

Sponsored Listings
Get Your SCJP kit free
Are you Intelligent!!!!

0 comments:

About This Blog

This Blog is all about Java and programming.This blog is written by Vaibhav Pandey(Read More about him) .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