Home >Java >javaTutorial >StringBuffer Class in Java
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.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
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.
Here are some points which show how we can use StringBuffer in java.
The following are String buffer constructors:
Here is the declaration of StringBuffer Class:
public final class StringBuffer extends Object implements Serializable,CharacterSequence,Appendable
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. |
Here are some of the examples of StringBuffer class which are given below:
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:
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:
In the above example, we have seen how to create a StringBuffer class and usage of its methods.
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.
The above is the detailed content of StringBuffer Class in Java. For more information, please follow other related articles on the PHP Chinese website!