Learn StringBuffer() Class in Java: Lecture 1

StringBuffer() Class in Java

The Java StringBuffer Class is just like the String Class, but with the difference that the StringBuffer class is mutable or can be modified. Whereas, the String class cannot be modified, in other words it is immutable.

The string that can be altered or changed or modified is known as a mutable string and the StringBuffer and StringBuilder classes are used to create mutable strings. As we know, String objects are immutable and if we want to make many changes or modifications with String objects, then we will end up with a memory leak. So, the StringBuffer() class allows us to make many modifications to our strings.

The following are some of the advantages of the StringBuffer() class.

  • Can be changed dynamically.
  • Is thread safe, in other words multiple threads cannot access it simultaneously. Hence, it is safe and will result in an order.
  • Every StringBuffer has a capacity.
  • It contains some specified character sequences that cannot be changed, but the length and content of the sequence can be changed by calling certain methods.
  • Strings can be obtained from String buffers.

There are four constructors used by the StringBuffer() Class.

  • StringBuffer()
  • StringBuffer(String str)
  • StringBuffer(int capacity)
  • StringBuffer(CharSequence []ch)

StringBuffer() constructs an empty string buffer with an initial capacity of 16 characters.

StringBuffer(String str) constructs a string buffer with the same characters as specified in charSequence.

StringBuffer(int capacity) constructs an empty string buffer with specified initial capacity as length.

StringBuffer(CharSequence []ch) constructs a string buffer initialized to the contents of the specified string.

Java StringBuffer() class Methods and their description

Methods & Description Method's Variants
StringBuffer append(): appends the specified string with the given string. append(String str), append(boolean b) , append(double d), append(float f), append(int i), append(long lng), append(Object obj), append(StringBuffer sb), append(char c), append(char[] str), append(char[] str, int offset, int len), append(CharSequence s), append(CharSequence s, int start, int end), appendCodePoint(int codepoint).
StringBuffer delete(): deletes the string from a given string on the basis of index number. delete(int startIndex, int endIndex), deleteCharAt(int index)
StringBuffer replace(): replaces the characters or strings with the substrings in the given string. replace(int startIndex, int endIndex, string str)
StringBuffer reverse(): replaces the sequence with the reverse sequence. ---
StringBuffer insert(): inserts the string within a given string at the specified position. insert(int offset, String str), insert(int offset, Object obj), insert(int offset, boolean b), insert(int offset, char c), insert(int offset, char[] str), insert(int index, char[] str, int offset, int len), insert(int dstOffset, CharSequence s, int startIndex, endIndex), insert(int offset, double d), insert(int offset, float f), insert(int i), insert(long lng).
int capacity(): returns the current capacity of the buffer. ---
void ensureCapacity(): ensures that the current capacity is at least equal to the specified minimum capacity. ensureCapacity(int minimumCapacity)
StringBuffer substring(): gets or returns the substring from the specified index number. substring(int startIndex), substring(int startIndex, int endIndex)
int indexOf(): returns the index within the string of the first occurrence of the specified substring. indexOf(String str), indexOf(String str, int fromIndex)
int lastIndexOf(): returns the index within the string of the last occurrence of the specified substring. lastIndexOf(String str), lastIndexOf(String str, int fromIndex)
char charAt(): returns the character value at the specified index. charAt(int index)
void chars(): copies the characters from the given sequence to the destination character array. chars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
void setCharAt(): sets the character at the specified index to ch. setCharAt(int index, char ch)
void setLength(): sets the length of the character sequence. setLength(int newLength)
CharSequence subsequence(): returns the new sequence that is a subsequence of the given sequence. CharSequence subsequence(int start, int end)
int codePointAt(): returns the character Unicode code point at a specified index. codePointAt(in index)
int codePointBefore(): returns the character Unicode code point before a specified index. codePointBefore(int index)
int codePointCount(): returns the number of Unicode code points in the specified text range of the given sequence. codePointCount(int beginIndex, int endIndex)
int length(): returns the length (character counts) of the string. ---
int offsetByCodePoints(): returns the index within the sequence that is offset from the given index by codePointOffset code points. offsetByCodePoints(int index, int codePointOffset)
void trimToSize(): reduces the storage taken by the character sequence. ---
String toString(): returns the string representing the data in the given sequence. ---

The following are the important methods of the StringBuffer() class that will be discussed first in the next lecture.



The following are the methods that are also found in the String class and they are discussed in my previous article series String class.

  • int length()
  • indexOf()
  • substring()
  • int charAt()
  • lastIndexOf()

The next lecture is right here, just click the link below.

 
Thank you, keep learning and sharing.

Up Next
    Ebook Download
    View all
    Learn
    View all