In this article, we will discuss StringBuffer class and its important methods. This class is helpful for string handling purposes.
Q) What is the need of StringBuffer class, when already String class is present in Java hierarchy?
String class:
- String is immutable means once new string literal is created cannot be changed for modification or altercation
- And for every modification (either addition or deletion) –> a new string is formed
- Thus, it leads for memory inefficient and memory leaks
- For example, for every string concatenation operation, a new string literal is created
- But it is thread-safe, as string once initialized cannot be modified
1. StringBuffer class:
- This is very similar to String class with only difference is, it is mutable
- That’s once StringBuffer is created, it can be altered or modfied, as and when required
- So, whenever there are lot of changes on the same string then StringBuffer is the best suit or replacement for String
- Also, it is thread-safe which means multiple threads cannot operate on the same StringBuffer simultaneously
- It has various useful methods similar to String class for various operations like appending, inserting, finding length, deleting, replacing, reversing and getting substring
Let’s us go through constructor and methods
2. StringBuffer Constructors:
StringBuffer Constructor |
Description |
public StringBuffer(); | Constructs a StringBuffer object with no characters inside in it
Initial capacity of 16 characters long |
public StringBuffer(int capacity); | Constructs a StringBuffer object with no characters inside in it
Initial capacity will be as specified in the constructor-argument |
public StringBuffer(String str); | Constructs a StringBuffer object with contents initialized to specified string in it
Initial capacity is 16 characters long + length of the string argument passed |
public StringBuffer(CharSequence seq); | Constructs a StringBuffer object with contents initialized to specified CharSequence in it
Initial capacity is 16 characters long + length of the CharSequence argument passed |
3. StringBuffer Methods:
- StringBuffer append(DataType dataType) : returns StringBuffer objects after appending respective data-type to the end of StringBuffer object. Internally data-types are converted to string 1st and then it is appended at the end of StringBuffer
- int capacity() : returns current capacity
- char charAt(int index) : returns char value for the specified index-position
- StringBuffer delete(int start, int end) : removes substring, starting from the start-index to end-index. Start-index is inclusive whereas end-index is exclusive
- StringBuffer deleteCharAt(int index) : deletes a single character at the specified index-position
- void ensureCapacity(int minimumCapacity) : ensures that the minimum capacity is at least equal to specified capacity in the method-argument
- int indexOf(String str) : returns index-position of 1st occurrence of specified string. There is another variant of this method which allows to specify start-position
- StringBuffer insert(int offset, DataType dataType) : very similar to append method with only difference is that, it inserts specified data-type at the specified offset-position whereas append method add specified data-type at the end of buffer, by default
- int lastIndexOf(String str) : returns index-position of last occurrence of specified string starting from right-hand side. There is another variant of this method which allows to specify start-position
- int length() : returns length of the invoking StringBuffer object
- StringBuffer replace(int start, int end, String str) : replaces the StringBuffer contents from specified start index-position to end index-position with the specified string. If required, the sequence will be lengthened to accommodate more characters
- StringBuffer reverse() : this method helps to reverse the invoking StringBuffer object
- CharSequence subsequence(int start, int end) : returns sub-sequence starting from specified index-position till specified end index-position
- String substring(int beginIndex) : returns substring starting from specified index-position till length
- String substring(int beginIndex, int endIndex) : returns substring in between begin index-position and end index-position
Related Articles:
- StringBuffer class
- StringBuffer append() method (13)
- StringBuffer capacity() method
- StringBuffer charAt(int index) method
- StringBuffer delete(int start, int end) method
- StringBuffer deleteCharAt(int index) method
- StringBuffer ensureCapacity(int minimumCapacity) method
- StringBuffer indexOf() method (2)
- StringBuffer insert() method (12)
- StringBuffer lastIndexOf() method (2)
- StringBuffer length() method
- StringBuffer replace(int start, int end, String str) method
- StringBuffer reverse() method
- StringBuffer substring(int start, int end) method
References:
- https://docs.oracle.com/javase/tutorial/essential/concurrency/immutable.html
- https://docs.oracle.com/javase/tutorial/essential/concurrency/imstrat.html
- https://docs.oracle.com/javase/tutorial/java/data/strings.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/class-use/String.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuffer.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html
Happy Coding !!
Happy Learning !!