What is difference between String,StringBuffer and StringBuilder
The StringBuffer and StringBuilder classes are used when there is a necessity to make alot of modifications to Strings of characters.
Unlike Strings objects of type StringBuffer and Stringbuilder can be modified over and over again with out leaving behind alot of new unused objects.
The StringBuilder class was introduced as of Java 5 and the main difference between the StringBuffer and StringBuilder is that StringBuilders methods are not thread safe(not Synchronised).
It is recommended to use StringBuilder whenever possible because it is faster than StringBuffer. However if thread safety is necessary the best option is StringBuffer objects.
Unlike Strings objects of type StringBuffer and Stringbuilder can be modified over and over again with out leaving behind alot of new unused objects.
The StringBuilder class was introduced as of Java 5 and the main difference between the StringBuffer and StringBuilder is that StringBuilders methods are not thread safe(not Synchronised).
It is recommended to use StringBuilder whenever possible because it is faster than StringBuffer. However if thread safety is necessary the best option is StringBuffer objects.
Example:
public class Test{ public static void main(String args[]){ StringBuffer sBuffer = new StringBuffer(" test"); sBuffer.append(" String Buffer"); System.ou.println(sBuffer); } }This would produce following result:
test String Buffer
Comments
Post a Comment