Which method of StringBuffer class is used to?

Which method of StringBuffer class is used to?

As we know that there are basically two types of java objects, they are mutable and immutable. Immutable objects are those objects whose contents cannot be modified once created. Whenever the content of an immutable object is changed, there will be the creation of new objects. In the case of a mutable object, we can change the contents of an existing object which does not result in the creation of a new object. Therefore mutable strings are those strings whose content can be changed without creating new objects.

StringBuffer and StringBuilder are mutable versions of String in java, whereas the java String class is immutable. Therefore StringBuffer is a mutable String used to provide mutability to String Objects. String class contains a fixed-length, immutable sequence of characters, whereas string buffer has an expandable length and writable sequence of characters.

How to Use String Buffer Class in Java?

Here are some points which show how we can use StringBuffer in java.

  • As already covered, the mutable string in java can be created using StringBuffer and StringBuilder classes.
  • The main difference between the two is that StringBuffer is a thread-safe implementation, whereas StringBuilder is not.
  • Whenever high performance and high security is required, one should prefer mutable versions of the String class.
  • Because of the String pool, there are security issues with the String class; therefore, StringBuffer is used whenever data security is necessary.
  • StringBuffer is better in terms of performance than StringBuffer as StringBuffer is thread-safe, but whenever thread safety is necessary, one should go for StringBuffer.

StringBuffer Constructors

The following are String buffer constructors:

  • StringBuffer(): This creates an empty StringBuffer with a default capacity of 16 characters.
  • StringBuffer(int capacity): This creates an empty StringBuffer with a specified capacity.
  • StringBuffer(CharSequence charseq): This creates StringBuffer containing the same characters as in the specified character sequence.
  • StringBuffer(String str): Creates a StringBuffer corresponding to specified String.

Here is the declaration of StringBuffer Class:

public final class StringBuffer extends Object implements Serializable,CharacterSequence,Appendable

Methods of StringBuffer Class in Java

Now we will see what different methods and fields available in StringBuffer are. Here is the list of commonly used methods available in the StringBuffer class:

Method Name Description
length() and capacity() The length of a mutable string can be calculated using the length() method, and corresponding capacity can be calculated using capacity().
append(String str)

append(int number)

This method is used for adding new text at the end of an existing string buffer.
insert(int index, String str)

insert(int index, char ch)

Used for inserting text at a specified position in a given string. In the given syntax index specifies the starting index of at which the string will be inserted.
reverse() Used for reversing the order of character in a given string buffer object.
delete(int start, int end) and deleteCharAt(int index) Used for deleting characters from a string buffer. Start indicates the starting index of the first character to be removed, and the end indicates an index of one past the last character to be removed.
replace(int startindex, int endindex, String str) Used for replacing character sequence between startindex and endindex-1 with the specified string buffer.
charAt(int index) Used to return character at the specified index in String buffer.
codePointAt(int index) Used to return the Unicode of character at the specified index.
codePointBefore(int index) Used to return the Unicode of character before the specified index.
substring(int start)

substring(int start, int end)

Used to return a new String that contains a subsequence of characters contained in a given string.
ensureCapacity(int capacity) Used for increasing the capacity of an existing string buffer object.
toString() Used to convert mutable string buffer to an immutable string object.

Examples of StringBuffer Class in Java

Here are some of the examples of StringBuffer class which are given below:

Example #1

Let us see a basic example of the StringBuffer class.

Code:

public class StringBufferDemo{
public static void main(String args[]){
StringBuffer sBuffer1=new StringBuffer("Welcome");
System.out.println("Original String is ::: " + sBuffer1 + ":: having length " + sBuffer1.length());
//using append method
sBuffer1.append(" To Edubca");
System.out.println("Modified String after append is :: " + sBuffer1 + " :: having length " + sBuffer1.length());
//using reverse method
sBuffer1.reverse();
System.out.println("Modified String after Reverse is :: " + sBuffer1);
}
}

The above code shows the creation of java StringBuffer and its different methods. The following output will be produced.

Output:

Which method of StringBuffer class is used to?

Example #2

In this example, we will see some more methods of the StringBuffer class.

Code:

public class StringBufferDemo{
public static void main(String args[]){
StringBuffer sBuffer=new StringBuffer ("WelcomeToEdubca");
System.out.println("Original String is ::: " + sBuffer + ":: having length " + sBuffer.length());
//using replace method
sBuffer.replace(0,9,"This is ");
System.out.println("Modified String after replace is :: " + sBuffer + " :: having length " + sBuffer.length());
//using delete method
sBuffer.delete(0,7);
System.out.println("Modified String after delete is :: " + sBuffer);
}
}

The above code will display the following as output.

 Output:

Which method of StringBuffer class is used to?

In the above example, we have seen how to create a StringBuffer class and usage of its methods.

Conclusion

From the above discussion, we have a clear understanding of StringBuffer in java, how it is created, and the different methods available in the StringBuffer class. Also, StringBuffer is thread-safe; therefore, it can be used in a multithreading environment.

This is a guide to StringBuffer Class in Java. Here we discuss the constructors, methods, and how to use the StringBuffer class in java, along with an example and code implementation. You can also go through our other suggested articles to learn more –

  1. Static Constructor in Java
  2. Conversion in Java
  3. Java String Operators
  4. StringBuilder Class in Java

Which of this method of class StringBuffer is used to concatenate the String representation to the end of invoking String?

Explanation: append() method of class StringBuffer is used to concatenate the string representation to the end of invoking string.

Which of these methods of class StringBuffer is used to extract a substring from a String object?

The substring(int start) method of StringBuffer class is the inbuilt method used to return a substring start from index start and extends to end of this sequence.

Which of this method of class StringBuffer is used to reverse sequence of characters?

In order to reverse the sequence of characters in a StringBuffer Object, we use the reverse() method.

Which method can be used to set the length of the buffer within a StringBuffer object?

The setLength(int newLength) method is used to change the length of the StringBuffer Object. It sets the length of the character sequence. The character sequence is updated to a new one whose length is determined by the value passed as the parameter in the method.