StringBuilder is identical to StringBuffer except for one important difference it is not synchronized, which means it is not thread safe. Its because StringBuilder methods are not synchronised.
StringBuilder Constructors
- StringBuilder ( ), creates an empty StringBuilder and reserves room for 16 characters.
- StringBuilder ( int size ), create an empty string and takes an integer argument to set capacity of the buffer.
- StringBuilder ( String str ), create a StringBuilder object and initialize it with string str.
Difference between StringBuffer and StringBuilder class
| StringBuffer class | StringBuilder class |
|---|---|
| StringBuffer is synchronized. | StringBuilder is not synchronized. |
| Because of synchronisation, StringBuffer operation is slower than StringBuilder. | StringBuilder operates faster. |
Example of StringBuilder
class Test {
public static void main(String args[])
{
StringBuilder str = new StringBuilder("study");
str.append( "tonight" );
System.out.println(str);
str.replace( 6, 13, "today");
System.out.println(str);
str.reverse();
System.out.println(str);
str.replace( 6, 13, "today");
}
}
Output :
studytonight studyttoday yadottyduts
No comments:
Post a Comment