Difference Between String , StringBuilder And StringBuffer Classes With Example : Java
2017-01-13 19:49
531 查看
Today we are going to understand the difference between String
, StringBuilder and StringBuffer . As you will find that there are minor differences between the above mentioned classes.
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.
String demo = " hello " ;
// The above object is stored in constant string pool and its value can not be modified.
demo="Bye" ; //new "Bye" string is created in constant pool and referenced by the demo variable
// "hello" string still exists in string constant pool and its value is not overrided but we lost reference to the "hello"string
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 synchronizedthat 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.
StringBuffer value can be changed , it means it can be assigned to the new value . Nowadays its a most common interview question ,the differences between the
above classes .
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
----------------------------------------------------------------------------------------------------
String
StringBuffer
StringBuilder
----------------------------------------------------------------------------------------------------
Storage Area | Constant String Pool
Heap Heap
Modifiable | No (immutable) Yes(
mutable ) Yes( mutable )
Thread Safe | Yes
Yes No
Performance | Fast
Very slow Fast
---------------------------------------------------------------------------------------------------
Please mention in the comments in case you have any doubts related to the post: difference between string, stringbuffer and stringbuilder.
, StringBuilder and StringBuffer . As you will find that there are minor differences between the above mentioned classes.
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.
String demo = " hello " ;
// The above object is stored in constant string pool and its value can not be modified.
demo="Bye" ; //new "Bye" string is created in constant pool and referenced by the demo variable
// "hello" string still exists in string constant pool and its value is not overrided but we lost reference to the "hello"string
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 synchronizedthat 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.
StringBuffer value can be changed , it means it can be assigned to the new value . Nowadays its a most common interview question ,the differences between the
above classes .
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
----------------------------------------------------------------------------------------------------
String
StringBuffer
StringBuilder
----------------------------------------------------------------------------------------------------
Storage Area | Constant String Pool
Heap Heap
Modifiable | No (immutable) Yes(
mutable ) Yes( mutable )
Thread Safe | Yes
Yes No
Performance | Fast
Very slow Fast
---------------------------------------------------------------------------------------------------
Please mention in the comments in case you have any doubts related to the post: difference between string, stringbuffer and stringbuilder.
相关文章推荐
- Difference Between String , StringBuilder And StringBuffer Classes
- The differences between String, StringBuffer and StringBuilder
- The differences between Java EE components and "standard" Java classes
- String and some of the difference between StringBuffer
- Difference between throw and throws in Exception handling - Java Example
- Difference between Truncate and Delete command in SQL - Interview Questions with Example
- Java - String, StringBuffer and StringBuilder
- The difference between scheduleAtFixedRate and scheduleWithFixedDelay in JAVA
- Part 59 to 60 Difference between Convert ToString and ToString,String and StringBuilder
- Java - String, StringBuilder and StringBuffer
- Java String StringBuffer and StringTokenizer
- Difference between Vector and ArrayList in java?
- Difference between Externalizable and Serializable in Java
- Java-What is the difference between <? super E> and <? extends E>?
- 黑马程序员_Java学习日记_JAVA中API中对象String和StringBuffer/StringBuilder
- the difference between final and static in java
- String and StringBuffer and StringBuilder的区别
- Difference between MappedByteBuffer and ByteBuffer.allocateDirect()
- The Differences Between C++ and Java
- The difference of overriding between C# and Java