StringBuilder & StringBuffer are the mutable classes available in java.lang package. They can be thought of an alternative to String when we need to perform a lot of modifications to a String value. Both these classes support same set of operations but unlike StringBuilder ,the StringBuffer class is thread-safe i.e the operations performed by different threads are synchronised.
An important point to understand here is String class is also thread-safe as it is immutable and so a thread can never change its state.
StringBuffer class is not mentioned in the objectives of OCA Java 8 Exam .So, we will be discussing only StringBuilder class moving forward in the post. However, all you need to understand is that there isn’t much difference among these two available choices. The main difference is thread synchronization available in StringBuffer implementation.
StringBuilder class provides below set of constructors to help instantiate its object.
// initial capacity= 16(default) + length(str) StringBuilder(String str) // initial capacity= 16(default) + length(charSeq) StringBuilder(CharSequence charSeq) // no content, initial capacity=given capacity(must be >0) StringBuilder(int capacity) // no content, initial capacity=16 StringBuilder()
The default capacity of a StringBuilder object is 16 characters. If any char sequence or String is passed to its constructor , then the initial capacity becomes 16 plus the length of that char sequence or string.
The StringBuilder class overrides the toString() method from the Object class. It returns the contents of a StringBuilder object as a String object.
String strValue = strBuilder.toString()
Some of the methods in the StringBuilder class are exactly the same as that of String class as they both come from CharSequence interface so I’ll skip the details about those methods. For such methods, I’ll list only the names.
int length()
char charAt(int index)
void setCharAt(int index, char ch)
StringBuilder deleteCharAt(int index)
CharSequence subSequence(int start, int end)
Note that this method returns a CharSequence and doesn’t modify the existing StringBuilder value.
StringBuilder sbr = new StringBuilder("Hello"); System.out.println(sbr.subSequence(2,3)); //prints l System.out.println(sbr); //prints Hello
/* * Note that it takes an Object parameter */ StringBuilder append(Object obj) StringBuilder append(CharSequence chSeq, int start, int end) StringBuilder append(char[] chArr, int offset, int length)
The object parameter is converted to its String equivalent by a method String.valueOf(obj)and then the resulting string is appended to the current value. String.valueOf() method internally invokes the toString() method of that class.
StringBuilder insert(int offset, Object obj) StringBuilder insert(int offset, CharSequence chSeq, int start, int end)
Above are some of the available constructors of insert() method. offset is used to specify the position at which we need to insert the given value. start and end specifies the range of chSeq to be inserted.
StringBuilder delete(int start, int end)
StringBuilder reverse()
int capacity()
void ensureCapacity(int minCapacity)
void trimToSize()
StringBuilder replace(int start, int end, String str)
For instance,
StringBuilder sbr = new StringBuilder("ABCDE"); sbr.replace(2,4,"01"); System.out.println(sbr); //prints AB01E
If you are following with me through this post, you might have noticed that the methods in the StringBuilder class like insert(),append(),deleteCharAt() etc returns a reference to modified StringBuilder object despite the fact that they all modify the state of the object itself.
For instance, the below code simply works fine.
StringBuilder sbr = new StringBuilder("A"); sbr.append("B"); System.out.println(sbr); //prints AB
Then why do these methods return a StringBuilder reference.The answer to this is that it helps us implement chain of methods.You can refer below example to understand it well.
StringBuilder sbr = new StringBuilder("Hello Tracy"); sbr.append("A").deleteCharAt(1).delete(1,2).insert(1,"Z"); System.out.println(sbr); //prints "HZlo TracyA"
Apart from the fact that StringBuilder is mutable, there are other couple of difference between these two classes that are noteworthy.