☰
StringBuilder objects are like String objects, except that they can be modified. Hence Java StringBuilder class is also used to create mutable (modifiable) string object. StringBuilder is same as StringBuffer except for one important difference. StringBuilder is not synchronized, which means it is not thread safe. At any point, the length and content of the sequence can be changed through method invocations.
StringBuilder class provides an API compatible with StringBuffer, but with no guarantee of synchronization. This class is designed for use as a drop-in replacement for StringBuffer in places where the string buffer was being used by a single thread. Where possible, it is recommended that this class be used in preference to StringBuffer as it will be faster under most implementations.
Instances of StringBuilder are not safe for use by multiple threads. If such synchronization is required then it is recommended that StringBuffer be used.
The append() method concatenates the given argument(string representation) to the end of the invoking StringBuilder object. StringBuilder class has several overloaded append() method. Few are:
StringBuilder strBuilder = new StringBuilder("Core");
strBuilder.append("JavaGuru");
System.out.println(strBuilder);
strBuilder.append(101);
System.out.println(strBuilder);
Output:
CoreJavaGuru
CoreJavaGuru101
Read more »
The insert() method inserts the given argument(string representation) into the invoking StringBuilder object at the given position.
StringBuilder strBuilder=new StringBuilder ("Core");
strBuilder.insert(1,"Java");
System.out.println(strBuilder);
Output:
CJavaore
The replace() method replaces the string from specified start index to the end index.
StringBuilder strBuilder=new StringBuilder("Core");
strBuilder.replace( 2, 4, "Java");
System.out.println(strBuilder);
Output:
CoJava
This method reverses the characters within a StringBuilder object.
StringBuilder strBuilder=new StringBuilder("Core");
strBuilder.reverse();
System.out.println(strBuilder);
Output:
eroC
The delete() method of StringBuilder class deletes the string from the specified beginIndex to endIndex.
StringBuilder strBuilder=new StringBuilder("Core");
strBuilder.delete( 2, 4);
System.out.println(strBuilder);
Output:
Co
The capacity() method returns the current capacity of StringBuilder object. The capacity is the amount of storage available for newly inserted characters, beyond which an allocation will occur.
StringBuilder strBuilder=new StringBuilder();
System.out.println(strBuilder.capacity());
strBuilder.append("1234");
System.out.println(strBuilder.capacity());
strBuilder.append("123456789112");
System.out.println(strBuilder.capacity());
strBuilder.append("1");
System.out.println(strBuilder.capacity()); //(oldcapacity*2)+2
StringBuilder strBuilder2=new StringBuilder("1234");
System.out.println(strBuilder2.capacity());
Output:
16
16
16
34
20
Observe how the capacity changes.
The ensureCapacity() method of StringBuilder class ensures that the given capacity is the minimum to the current capacity. If the current capacity is less than the argument, then a new internal array is allocated with greater capacity. The new capacity is the larger of:
If the minimumCapacity argument is nonpositive, this method takes no action and simply returns.
StringBuilder strBuilder=new StringBuilder("Core");
strBuilder.ensureCapacity(10);
System.out.println(strBuilder.capacity());
StringBuilder strBuilder2=new StringBuilder("Core");
strBuilder2.ensureCapacity(30);
System.out.println(strBuilder2.capacity());
Output:
20
42
Some of differences between StringBuffer and StringBuilder are given below:
| StringBuffer | StringBuilder |
|---|---|
| StringBuffer is synchronized i.e. thread safe. | StringBuilder is non-synchronized i.e. not thread safe. |
| StringBuffer is less efficient and slower than StringBuilder as StringBuffer is synchronized. | StringBuilder is more efficient and faster than StringBuffer. |
| StringBuffer is old, its there in JDK from very first release. | StringBuilder is introduced much later in release of JDK 1.5 |