In previous post, we learned few important things about String class in Java. In this post, we will see, what is StringBuffer class, how to create a StringBuffer object and few important methods of StringBuffer class.
StringBuffer is a part of java.lang package, which is similar to that of String class, but is non-immutable. Which means, we can alter the object value of StringBuffer object after creating it, at any time. StringBuffer contains every methods synchronized, so which makes it thread safe.
Following is a simple example of creating an object of StringBuffer class. Here, two String literals are created in String constant pool(SCP) for “ASB” and “Notebook”. Also a String buffer object is created, which will hold the value “ASB Notebook”, as the created object is modified with append method in the next statement. StringBuffer internally creates an char object internally to hold the characters, so totally at least 4 objects are created.
StringBuffer sb = new StringBuffer("ASB"); //Creates 3 objects : for String literal "ASB" in SCP, another 2 in heap(one for StringBuilder and another for char). sb.append("Notebook"); //Creates another String literal in SCP.
Initially, StringBuffer object is created with a character capacity of default value 16. If the capacity increases initial capacity, it will add additional capacity with new capacity = (current capacity + 1) * 2. Example, once initial default capacity is exceeded, capacity will be automatically extended to 34 and so on(using formula : (16+1) * 2). Following are few examples about capacity values, while creating StringBuffer object.
StringBuffer sb1 = new StringBuffer(); //capacity value 16(default) StringBuffer sb2 = new StringBuffer("ASB"); //Capacity of the String buffer object is now calculated as (default capacity + length of given String), i.e 16+3 = 19
Constructors of StringBuffer class
Following are the few constructors of StringBuffer class.
- StringBuffer() : Creates an empty object with capacity of 16 characters.
- StringBuffer(CharSequence seq) : Constructs a StringBuffer object using given input Character sequence.
- StringBuffer(int capacity) : Constructs a StringBuffer object with given capacity.
- StringBuffer(String str) : Constructs a StringBuffer object with given input String object, with capacity value of string length + default capacity value(16).
Few important methods of StringBuffer class
- public int capacity() : Returns current capacity of string buffer object.
- public StringBuffer append(String str) : Appends the given string at the last of current StringBuffer object. Append method is also available for different data types, like boolean, char, int,etc.
- public int length() : Returns length of StringBuffer characters.
- public char charAt(int specifiedIndex) : Returns character present in the given index value.
- public void setCharAt(int index, char newChar) : Replaces the character present in given index with given character.
- public String substring(int start, int end) : Returns sub string value from start and end index values.
- public StringBuffer insert(int index, String str) : Inserts given string to the given index position.
- public StringBuffer delete(int begin, int last) : Deletes the characters between given begin and end index values.
- public StringBuffer reverse() : Reverses the given String buffer characters.
In this post, we learned about StringBuffer class in Java, it’s constructors, initial capacity of StringBuffer object and important methods.
In real world scenario’s StringBuffer class is not used in most of the cases, as StringBuffer class contains all the methods synchronized, which degrades the performance of the application, as multiple threads using the same StringBuffer object will keep on waiting until one thread completes it’s operation. It is recommended to use StringBuilder in case there is no need of thread safety.