String :
Consider the bellow program first,
String s = new String(" Manoj");
s.concat("Patra");
System.out.println(s); //Manoj
String s = new String(" Manoj");
s.concat("Patra");
System.out.println(s); //Manoj
- String objects are immutable i.e once we created a String object we can't perform any changes in the existing object.
- If we try to perform any change with those change a new object will be created.
- In the first line of the above program we are creating a string object 's' which contains 'Manoj'
- In the second line we are trying to do some change so, another new object will be created with content 'ManojPatra'
- In 3rd line we are printing 's'. Since 's' is still pointing to old object only. It will print 'Manoj'.
Summery : Once we create a string object we can't perform any changes in the existing object. If we are trying to perform any changes with those changes a new object will be created. This non changeable nature is nothing but immutability of string objects.
StringBuffer :
Consider the bellow program first,
StringBuffer sb = new StringBuffer(" Manoj");
sb.append("Patra");
System.out.println(s); //ManojPatra
StringBuffer sb = new StringBuffer(" Manoj");
sb.append("Patra");
System.out.println(s); //ManojPatra
- StringBuffer objects are mutable i.e after creating a StringBuffer object we can perform any changes in the existing object.
- If we perform any changes , all those changes will be reflected in the same object.
- In the first line of the above program we are creating a StringBuffer object 'sb' which contains 'Manoj'
- In the second line we are trying to do some change so, all changes will be performed on same object.
- In 3rd line we are printing 'sb'. It will print 'ManojPatra'.
Summery : Once we create a string object we can perform any type of changes in the existing object. This changeable nature is nothing but mutability of StringBuffer object.
Difference Between StringBuffer and StringBuilder?
StringBuffer is exactly same as StringBuilder ( including Methods and Construvtor) except the following difference.
StringBuffer :
- Every method present in StringBuffer is synchronized.
- At a time only one thread is allowed to operate on StringBuffer object. Hence StringBuffer object is thread safe.
- It increases waiting time of threads and hence relatively performance is low.
- Introduced in 1.0 version.
StringBuilder :
- No method present in StringBuilder is synchronized.
- At a time multiple threads are allowed to operate on StringBuilder object. Hence StringBuilder object is not thread safe.
- Threads are not required to wait to operate on StringBuilder object and hence performance is relatively high.
- Introduced in 1.5 version.
When to use String, StringBuffer and StringBuilder?
- If the content is fixed and won't change frequently then we should go for String.
- If the content is not fixed and keep on changing but Thread safety is required then we should go for StringBuffer.
- If the content is not fixed and keep on changing but Thread safety is NOT required then we should go for StringBuffer.