String v.s. StringBuffer
Author: vglass@jfind.com, Van Glass
When dealing with Strings whether you use String or StringBuffer to store String data can have a significant effect on your
overall performance. Today we will take a look at both the String and StringBuffer classes and identify those situations in which
a StringBuffer should be used instead of a String.
The String object simply stores an array of characters and provides methods to perform operations on that array. The String object is
also the only object which overrides the "+" concatenation operator. This allows for Strings to be created based on the concatenation
of one or more String objects.
e.g.
String fname = "John";
String lname = "Smith";
String fullname = fname + lname;
Strings however are immutable meaning they cannot be modified once created. Whenever you reassign the value of a String variable, in the background you are really
creating another String object and telling the JVM to use the newly created String object as the placeholder for a variable.
e.g.
String fname = "John";
/*
new String object is created
old object awaiting garbage collection
*/
fname = "Jack";
Consider a case where a String variable needs to be modified several times. Each time the value of that String variable is modified
a new String object must be created. This will result in several String objects in memory awaiting garbage collection. Not to mention the
additional CPU overhead of creating these new String objects.
e.g.
int i;
String contents = null;
for(i = 0; i < 10; i++)
{
contents += "hello world\n";
}
/* print contents to console */
System.out.println(contents);
The above code would create 10 seperate String objects 9 of which would reside in memory awaiting garbage collection. The more effecient way to handle this
is through the use of a StringBuffer object.
A StringBuffer object again stores an array of characters with some subtle differences.
1. It does not overload the "+" concatenation operator.
2. Its contents can be modified without creating a new StringBuffer instance.
Modifying the code above to take advantage of the StringBuffer object requires only a few simple changes.
e.g.
int i;
StringBuffer contents = new StringBuffer();
for(i = 0; i < 10; i++)
{
contents.append("hello world\n");
}
/* print contents to console */
System.out.println(contents.toString());
By using the StringBuffer object only one object is used during the lifespan of the for loop. The performance gains
may not be evident in a small loop such as above. However try doing this with a String object that you expect to be modified
hundreds of times and you will begin to notice the difference.
| Sponsored Links - please visit our sponsors |
| Learn Java Server Faces! | | Use WebGalileo Faces JSF components to quickly create Java based web apps. | | http://www.jscape.com/webgalileofaces/ |
| SSH Factory | | automate telnet and SSH tasks | | http://www.jscape.com/sshfactory/ |
| Secure FTP Applet | | Connect to FTP securely from within your browser. | | http://www.jscape.com/sftpapplet/ |
| FTP Applet | | Add file transfer capabilities to your web pages. | | http://www.jscape.com/ftpapplet/index.html |
| Java FTP Component | | Easily add FTP to your Java apps. | | http://www.jscape.com/inetfactory/ftp.html |
Sponsor this site
| |