Java – StringBuffer class

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:

  1. 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
  2. int capacity() : returns current capacity
  3. char charAt(int index) : returns char value for the specified index-position
  4. 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
  5. StringBuffer deleteCharAt(int index) : deletes a single character at the specified index-position
  6. void ensureCapacity(int minimumCapacity) : ensures that the minimum capacity is at least equal to specified capacity in the method-argument
  7. 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
  8. 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
  9. 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
  10. int length() : returns length of the invoking StringBuffer object
  11. 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
  12. StringBuffer reverse() : this method helps to reverse the invoking StringBuffer object
  13. CharSequence subsequence(int start, int end) : returns sub-sequence starting from specified index-position till specified end index-position
  14. String substring(int beginIndex) : returns substring starting from specified index-position till length
  15. String substring(int beginIndex, int endIndex) : returns substring in between begin index-position and end index-position

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java - StringBuffer append() method
Java - String valueOf() method