Home >Java >javaTutorial >Mutable String in Java
With Mutable string, we can change the contents of an existing object, which does not create a new object. Therefore mutable strings are those strings whose content can be changed without creating a new object. StringBuffer and StringBuilder are mutable versions of String in java, whereas the java String class is immutable. Immutable objects are those objects whose contents cannot be modified once created. Whenever an immutable object’s content is changed, there will be a creation of a new object.
ADVERTISEMENT Popular Course in this category JAVA MASTERY - Specialization | 78 Course Series | 15 Mock TestsAs 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 and StringBuilder are 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.
The following are the main constructors of string builder and string buffer classes.
The following are String buffer constructors:
Here is the declaration of StringBuffer Class:
public final class StringBuffer extends Object implements Serializable,CharacterSequence
The following are String builder constructors:
Here is the declaration of StringBuilder Class:
public final class StringBuilder extends Object implements Serializable,CharacterSequence
Now we will see what different methods and fields available in StringBuffer and StringBuilder classes are. Here is the list of commonly used methods available in these:
Methods of string mutable classes are given below:
|
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 mutable string. | ||||||||||||||
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. | ||||||||||||||
delete(int start, int end) and deleteCharAt(int index) | Used for deleting characters from a mutable string. Start indicates the starting index of the first character to be removed, and the end index 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. |
The above-listed methods are commonly used methods of StringBuffer and StringBuilder classes.
Examples of mutable string in java are given below:
Let us see a basic example of a StringBuffer class.
Code:
package com.edubca.mutablestringdemo; public class MutableStringDemo{ 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 how to use StringBuilder in java.
Code:
package com.edubca.mutablestringdemo; public class MutableStringDemo{ public static void main(String args[]){ StringBuilder sBuilder=new StringBuilder("WelcomeToEdubca"); System.out.println("Original String is ::: " + sBuilder + ":: having length " + sBuilder.length()); //using replace method sBuilder.replace(0,9,"This is "); System.out.println("Modified String after replace is :: " + sBuilder + " :: having length " + sBuilder.length()); //using delete method sBuilder.delete(0,7); System.out.println("Modified String after delete is :: " + sBuilder); } }
Output:
In the above example, we have seen how to createStringBuilder class and usage of its methods.
The above is the detailed content of Mutable String in Java. For more information, please follow other related articles on the PHP Chinese website!