Java: StringBuilder class

In this article, we will discuss StringBuilder class and its important methods. This class is helpful for string handling purposes, similar to StringBuffer.

StringBuilder is used to create mutable string objects very similar to StringBuffer.

 

What is the need of StringBuilder class, when StringBuffer class is present?

StringBuffer class:

  • It is mutable i.e.; it can be altered or modifier using append and insert operations
  • It is thread-safe, so multiple threads cannot operate on same StringBuffer object
  • Due to synchronization, all operations on StringBuffer object slows-down

StringBuilder class:

  • It is also mutable similar to StingBuffer
  • But it is non-synchronized i.e.; not thread-safe
  • Therefore, multiple threads can operate on same StringBuilder object
  • So, operations on StringBuilder object is faster when compared to StringBuffer

 

Let’s us go through constructor and methods

StringBuilder Constructors:

StringBuilder Constructor

Description

public StringBuilder(); Constructs a StringBuilder object with no characters inside in it

Initial capacity of 16 characters long

public StringBuilder(int capacity); Constructs a StringBuilder object with no characters inside in it

Initial capacity will be as specified in the constructor-argument

public StringBuilder(String str); Constructs a StringBuilder object with contents initialized to specified string in it
public StringBuilder(CharSequence seq); Constructs a StringBuilder object with contents initialized to specified CharSequence in it

 

StringBuilder Methods:

  1. StringBuilder append(DataType dataType) : returns StringBuilder objects after appending respective data-type to the end of StringBuilder object. Internally data-types are converted to string 1st and then it is appended at the end of StringBuilder
  2. int capacity() : returns current capacity
  3. char charAt(int index) : returns char value for the specified index-position
  4. StringBuilder delete(int start, int end) : removes substring starting from specified start-index to end-index. Start-index is inclusive whereas end-index is exclusive
  5. StringBuilder 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. StringBuilder 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 1st 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 StringBuilder object
  11. StringBuilder replace(int start, int end, String str) : replaces StringBuilder contents from specified start-index to end-index with specified string. If required, the sequence will be lengthened to accommodate more characters
  12. StringBuilder reverse() : this method helps to reverse the invoking StringBuilder
  13. CharSequence subsequence(int start, int end) : returns sub-sequence starting from specified start-index till specified end-index
  14. String substring(int beginIndex) : returns substring starting from specified index till length
  15. String substring(int beginIndex, int endIndex) : returns substring in between begin-index and end-index

 

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/6/docs/api/java/lang/String.html
https://docs.oracle.com/javase/7/docs/api/java/lang/String.html
https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
https://docs.oracle.com/javase/6/docs/api/java/lang/class-use/String.html
https://docs.oracle.com/javase/7/docs/api/java/lang/class-use/String.html
https://docs.oracle.com/javase/8/docs/api/java/lang/class-use/String.html
https://docs.oracle.com/javase/6/docs/api/java/lang/StringBuffer.html
https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuffer.html
https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuffer.html
https://docs.oracle.com/javase/6/docs/api/java/lang/StringBuilder.html
https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html
https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html

 

Happy Coding !!
Happy Learning !!

Java: StringBuilder append() method
Java: StringBuffer substring() method