Compare the Difference Between Similar Terms

Difference Between

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

Difference Between StringBuffer and StringBuilder

June 19, 2011Posted byIndika

生态ngBuffer 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类是在JDK 1.0中引入的。生态ngBuffer 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类是在JDK 1.5中引入的。生态ngBuilder 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:ProgrammingTagged With:Java,JDK 1.0,JDK 1.5,生态ng class,string classes in Java,生态ngBuffer,生态ngBuffer API,生态ngBuffer class,生态ngBuilder,生态ngBuilder API,生态ngBuilder 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 ReplyCancel 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 Apple iPhone 5 and HTC One X

Difference Between Apple iPhone 5 and HTC One X

Difference Between Motorola Atrix 4G and HTC Evo Shift 4G

Difference Between JPA and Hibernate

Difference Between Double Fertilization and Triple Fusion

Difference Between Double Fertilization and Triple Fusion

Difference Between Editorial and Glamour Photos

Latest Posts

  • What is the Difference Between Papules and Pustules
  • What is the Difference Between Ubiquinol and CoQ10
  • What is the Difference Between Ubiquinone and Plastoquinone
  • What is the Difference Between Heliophytes and Sciophytes
  • What is the Difference Between Alpha Arbutin and Vitamin C
  • What is the Difference Between Liquid Paraffin and Glycerin
  • Home
  • Vacancies
  • About
  • Request Article
  • Contact Us

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