Computersight > Programming > Java

Compare String, Stringbuffer and Stringbuilder

String is a group of characters. It is an immutable object.

What is the Immutable?

Immutable means unable to changed. So we can not change the contents of the String, once it created.

You can ask the following question:

String s = “The contents of the String.”;

s = s + “The Content will not be modified.”;

The value of the “s” is “The contents of the String. The Content will not be modified.” So the content of the string “s” has modified.

No, but that's not correct. The JVM will create the two objects here.

The first object referenced to “The contents of the String”.

The second object referenced to “The Content will not be modified”.

Whenever we use the “+” or “concat” operation on String, the JVM will create another object.

String s = “The contents of the String.”;

The JVM will create the Object here and “s” is the reference of that object.

s = s + “The Content will not be modified.”;

The JVM will copy the existing content of “s” reference to here and create the new Object. So the old Object will be garbage collected.

The “+” or concat operation is slow performance compare to “StringBuffer.concat” or “StringBuilder.concat” operation.

When to use plus operand?

See the following two examples;

String text = “Java is a object orient programn”+

“java is a robustn”+

“java is simplen”+

“java is multithread”;

StringBuilder text = new StringBuilder (Java is a object orient programn”);

text.concat(“java is a robustn”);

text.concat(“java is simplen”);

text.concat (“java is multithread”);

Could you guess which examples is better performance?

You may tell that the second example gives better performance. But that's not correct. The first example gives the better performance.

The first example will be converted like below, while compilation time.

String text =“java is a object orient progamnjava is a robustnjava is simplenjava is multithread”;

StringBuffer:

StringBuffer is also group of character, but it is a Mutable. So we can change the content of the StringBuffer without creating new object. When you use the “append” method, it will not create new object.

StringBuffer is a thread safe. So use the StringBuffer, if you need thread safe. Otherwise you can use the StringBuilder.

StringBuilder:

StringBuilder is also group of characters. But it is not a thread safe. It is faster than StringBuilder; because of it is not a thread safe. StringBuilder do everything, whatever StringBuffer do.

5
Liked It
I Like It!
Related Articles
Basic Java  |  Structures in Blitz
More Articles by gadangi
Story of the JDBC Drivers  |  The Generic for Loop
Latest Articles in Java
Basic Java  |  Story of the JDBC Drivers
Comments (0)
Post Your Comment:
Name:  
Copy the code into this box:  
Inside Computersight

Communication & Networks

 /

Computers

 /

Hardware

 /

Operating Systems

 /

Programming

 /

Software


Popular Tags
Popular Writers
Computersight
About Us
Terms of Use
Privacy Policy
Services
Submit an Article
Advertise with Us
Contact

© 2007 Copyright Stanza Ltd. All Rights Reserved.