The differences between String, StringBuffer and StringBuilder
来源:互联网 发布:淘宝摄影棚什么样的好 编辑:程序博客网 时间:2024/05/17 23:45
String
String is immutable(once created can not be changed) object. The object created as a String is stored in the Constant String Pool.
Every immutable object in Java is thread safe ,that implies String is also thread safe . String can not be used by two threads simultaneously. String once assigned can not be changed.
StringBuffer
StringBuffer is mutable means one can change the value of the object. The object created through StringBuffer is stored in the heap. StringBuffer has the same methods as the StringBuilder , but each method in StringBuffer is synchronized that is StringBuffer is thread safe.
Due to this it does not allow two threads to simultaneously access the same method . Each method can be accessed by one thread at a time .
But being thread safe has disadvantages too as the performance of the StringBuffer hits due to thread safe property . Thus StringBuilder is faster than the StringBuffer when calling the same methods of each class.
String Buffer can be converted to the string by using toString() method.
StringBuffer demo1 = new StringBuffer("Hello") ;
// The above object stored in heap and its value can be changed .
demo1=new StringBuffer("Bye");
// Above statement is right as it modifies the value which is allowed in the StringBuffer.
StringBuilder
StringBuilder is same as the StringBuffer , that is it stores the object in heap and it can also be modified . The main difference between the StringBuffer and StringBuilder is that StringBuilder is also not thread safe.
StringBuilder is fast as it is not thread safe .
StringBuilder demo2= new StringBuilder("Hello");
// The above object too is stored in the heap and its value can be modified
demo2=new StringBuilder("Bye");
// Above statement is right as it modifies the value which is allowed in the StringBuilder
public final class String implements java.io.Serializable, Comparable<String>, CharSequence { /** The value is used for character storage. */ private final char value[]; /** Cache the hash code for the string */private int hash; // Default to 0......}abstract class AbstractStringBuilder implements Appendable, CharSequence { /** * The value is used for character storage. */ char[] value; int count; AbstractStringBuilder() { } AbstractStringBuilder(int capacity) { value = new char[capacity];}......}public final class StringBuffer extends AbstractStringBuilder implements java.io.Serializable, CharSequence{ /** * A cache of the last value returned by toString. Cleared * whenever the StringBuffer is modified. */ private transient char[] toStringCache; /** use serialVersionUID from JDK 1.0.2 for interoperability */ static final long serialVersionUID = 3388685877147921107L; public StringBuffer() { super(16); } public StringBuffer(int capacity) { super(capacity); } public StringBuffer(String str) { super(str.length() + 16); append(str); } @Override public synchronized int length() { return count; } @Override public synchronized int capacity() { return value.length;}......}public final class StringBuilder extends AbstractStringBuilder implements java.io.Serializable, CharSequence{ /** use serialVersionUID for interoperability */ static final long serialVersionUID = 4383685877147921099L; public StringBuilder() { super(16); } public StringBuilder(int capacity) { super(capacity); } public StringBuilder(String str) { super(str.length() + 16); append(str); }
- The differences between String, StringBuffer and StringBuilder
- Differences between String and StringBuilder in Java
- [Java]Difference between String StringBuffer and StringBuilder
- Difference Between String , StringBuilder And StringBuffer Classes
- Difference Between String , StringBuilder And StringBuffer Classes With Example : Java
- String,StringBuilder and StringBuffer
- String, StringBuffer, and StringBuilder
- String、StringBuffer and StringBuilder
- String StringBuilder and StringBuffer
- The differences between BLOB and TEXT
- The differences between Direct3D9 And Direct3D10
- The Differences Between Java, C And C++
- The differences between 'alt' and 'title' !
- Differences Between the Import and Load Utility
- the differences between wait and waitpid
- the differences between Crystal and Oscillator
- The differences between implements and extends
- The Differences Between MBR and GPT
- 共用体中涉及的大端和小端存储模式
- View requires API level 11 (current min is 8): <CalendarView>
- 【Android UI】 Shape详解 (GradientDrawable)
- NVMe的优势
- [MFC]DlgDemo1程序:基本的模态对话框
- The differences between String, StringBuffer and StringBuilder
- 在需求分析中就可以避免的那些错误11
- 三目运算符的用法
- android 超高仿微信图片选择器
- HDU 2918 Tobo or not Tobo DFS
- C风格字符串和C++string对象的相互转化
- 将mysql数据导入redis
- autoconfig和automake的使用
- Spark入门实战系列--8.Spark MLlib(上)--机器学习及SparkMLlib简介