Difference between String, StringBuffer and StringBuilder in java - In depth coverage


Difference between String, StringBuffer and StringBuilder is a very important interview question. Its very basic question which every developer must know.

Here I have covered few in depth differences as well which will be handy for experienced developers.



Difference between String, StringBuffer and StringBuilder in java - In depth coverage


Property
String
StringBuffer
StringBuilder
1
Storage area
When String is created without using new operator, JVM will create string in string pool area of heap.

When String is created using new operator, it will force JVM to create new string in heap (not in string pool).


Example -->
String s1 = "abc";
> in string pool area of heap.


String s2 = new String("abc");
> in heap










StringBuffer is created in heap.










StringBuilder is created in heap.
2
mutable/ Immutable
String is immutable class in java, any changes made to Sting class produces new String.



Please see
example 1a and example 1b below.












StringBuffer is mutable class in java, any changes made to StringBuffer class won’t produces new String.

Please see
example 2 below.
StringBuilder is mutable class in java, any changes made to StringBuilder class won’t produces new String.



Please see
example 3 below.
3
String is immutable that makes it a thread -safe class.
StringBuffer methods are synchronized.

Means no 2 threads on same StringBuffer object cannot access methods concurrently.
StringBuilder methods are not synchronized.

Means 2 threads on same StringBuilder object can access methods concurrently.

Note : Methods of StringBuffer and StringBuilder are same, the only difference is of synchronization.
4
Performance
Value of String in String pool is cached, hence making it fast.

String created with new operator is also fast process.
Because of synchronized methods its slow.
Because of non synchronized methods its fast.
5
Internal working
Let’s say we have following statements -
String str = "abc" ;
str = str + "def";

Internally + operator uses StringBuffer for concatenating strings.

So,
String str = new StringBuilder(str).append("def").toString();
Internally StringBuffer adds new characters to previous StringBuffer.
Internally StringBuilder adds new characters to previous StringBuilder.
6
Introduced in
Java 1
jdk 1.0
Java 1
jdk 1.0
Java 5
jdk 1.5


/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */

String mutable/ Immutable

Example 1a -->

String str= "ab";
> No string with “ab” is there in string pool, so JVM will create string “ab” in string pool and str will be a reference variable which will refer to it.


str.concat("cd")
> cd will be concatenated with ab and new string  “abcd”  will be formed. No string with “abcd” is there in pool, so JVM will create string “abcd” in string pool, but there won’t be any reference variable to abcd”  (we are just using it only in syso statement), meanwhile str will still be pointing to “ab”.


System.out.println(str);
str is referring to  “aband string abcdwill be eligible for garbage collection.




String mutable/ Immutable

Example 1b -->
What will happen when below 2 statements will execute >

String str= "ab";
str = "abcd";

String str= "ab";
No string with “ab” is there in string pool, so JVM will create string “ab” in string pool and str will be a reference variable which will refer to it.


str = "abcd";
Now, No string with “abcd” is there in string pool, so JVM will create new string “abcd” in string pool and str will be a reference variable which will refer to it.

String "abcd" will stay in string pool but reference to it will be lost, and it will be eligible for garbage collection.




StringBuffer mutable/ Immutable

Example 2-->

StringBuffer sBuffer = new StringBuffer("ab") ;
>JVM will create stringBuffer object “ab” in heap and sBuffer will be a reference variable which will refer to it.


          sBuffer= sBuffer.append("cd");

>  “cd” will be added to StringBuffer object referred by sBuffer. So,   “abcdwill be formed.

(Note: addition was made to previous object, no new object was formed,
Behaviour was different as compared to immutable String’s concat function
)

System.out. println (sBuffer);
sBuffer is referring to  “abcd.



StringBuilder mutable/ Immutable


Example 3-->

StringBuilder sBuilder = new StringBuilder("ab") ;

>JVM will create stringBuilder object “ab” in heap and sBuilder will be a reference variable which will refer to it.

sBuilder=sBuilder. append("cd");
         
>  “cd” will be added to StringBuilder object referred by sBuilder. So,   “abcdwill be formed.

(Note: addition was made to previous object, no new object was formed,
Behaviour was different as compared to immutable String’s concat function
)


System.out.println(sBuilder);

sBuilder is referring to  “abcd.


RELATED LINKS>


String pool/ String literal pool/ String constant pool in java

No comments:

Post a Comment