Compare the Difference Between Similar Terms

Difference Between

Home / Technology / IT / Programming / Difference Between StringBuffer and StringBuilder

Difference Between StringBuffer and StringBuilder

June 19, 2011 Posted by Indika

StringBuffer vs StringBuilder

Java is a very popular object oriented language. In Java, the String class is provided to hold a sequence of characters that cannot be modified (once initialized). Alternatively, Java programming language provides two types of mutable sequences of characters. That is, when the programmers need to modify a certain String (after initialization), they need to use the StringBuffer class or the StringBuilder class, instead of the String class. StringBuffer was introduced in JDK 1.0 and StringBuilder class was introduced in JDK 1.5, actually as a replacement for StringBuffer class (for single-thread environments).

What is StringBuffer?

StringBuffer class was introduced in JDK 1.0. StringBuffer class belongs to the java.lang package and is inherited from the generic java.lang.object. Programmers cannot extend it further since It is a final class. StringBuffer class implements Serializable, Appendable and CharSequience interfaces. An object of the class StringBuffer can hold a sequence of characters that is mutable and thread-safe. That means, it is very much similar to a String object, but the sequence of characters (length and content) can be changed at any time after initializing the StringBuffer object. However, that should be done using the specific methods provided by the StringBuffer class. There are two principle operations in StringBuffer class. They are provided by append() and insert() methods. These methods are overloaded, so they are able to accept data of any type such as integer and long. Both methods firstly transform any input to a string, and then adds (appends or inserts) the characters of the corresponding string to the existing Stribbuffer object. The append() method adds the converted string to the end of the existing StringBuffer object, while insert() method will add the input characters to the specified insertion point.

What is StringBuilder?

StringBuilder class was introduced in JDK 1.5. StringBuilder API is very much similar to StringBuffer API. In fact, StringBuilder class was actually introduced as a replacement for the StringBuffer class (for single-thread applications). StringBuilder class belongs to the java.lang package and is inherited from the generic java.lang.object. It is a final class and so the programmers cannot extend it. StringBuilder class implements Serializable, Appendable and CharSequience interfaces. An object of the class StringBuilder can hold a sequence of characters that is mutable but not thread-safe. That means, it is very much similar to a String object, but the string can be changed at any time. But StringBuilder class does not provide synchronization, and therefore is claimed to be faster than using StringBuffer class. StringBuilder class provides append() and insert() methods with exactly similar functionality as in StringBuffer class.

What is the difference between StringBuffer and StringBuilder?

Although, StringBuilder and StringBuffer classes can be used for mutable sequences of characters in Java, they have a key difference. Unlike StringBuffer class, StringBuilder class is not thread-safe, and provides no synchronization. Therefore, it is recommended that the StringBuilder class should be used in place of StringBuffer class in single-thread applications, because it is claimed that StringBuilder class will be much faster than StringBuffer class (under normal circumstances).

Related posts:

Difference Between Enumeration and Iterator Difference Between JSP and Servlets Difference Between Objects and Classes Difference Between Object Oriented Programming and Procedural Programming Difference Between Encapsulation and Abstraction

Filed Under: Programming Tagged With: Java, JDK 1.0, JDK 1.5, String class, string classes in Java, StringBuffer, StringBuffer API, StringBuffer class, StringBuilder, StringBuilder API, StringBuilder class

About the Author: Indika

Indika, BSc.Eng, MSECE Computer Engineering, PhD. Computer Science, is an Assistant Professor and has research interests in the areas of Bioinformatics, Computational Biology, and Biomedical Natural Language Processing.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Request Article

Featured Posts

Difference Between Coronavirus and Cold Symptoms

Difference Between Coronavirus and Cold Symptoms

Difference Between Coronavirus and SARS

Difference Between Coronavirus and SARS

Difference Between Coronavirus and Influenza

Difference Between Coronavirus and Influenza

Difference Between Coronavirus and Covid 19

Difference Between Coronavirus and Covid 19

You May Like

Difference Between Doctor and Professor

Difference Between Doctor and Professor

Difference Between Flaccid and Spastic Paralysis

Difference Between Flaccid and Spastic Paralysis

Difference Between Etiology and Pathophysiology

Difference Between Etiology and Pathophysiology

What is the Difference Between Leiomyoma and Adenomyosis

What is the Difference Between Leiomyoma and Adenomyosis

What is the Difference Between Counterflow and Parallel Flow Heat Exchanger

What is the Difference Between Counterflow and Parallel Flow Heat Exchanger

Latest Posts

  • What is the Difference Between Foundation and BB Cream
  • What is the Difference Between Cybrids and Hybrids
  • What is the Difference Between Hapten and Adjuvant
  • What is the Difference Between Omphalocele and Gastroschisis
  • What is the Difference Between Autonomic and Somatic Reflexes
  • What is the Difference Between Plagiocephaly and Craniosynostosis
  • Home
  • Vacancies
  • About
  • Request Article
  • Contact Us

Copyright © 2010-2018 Difference Between. All rights reserved. Terms of Use and Privacy Policy: Legal.