Home  >  Article  >  Java  >  Mutable String in Java

Mutable String in Java

WBOY
WBOYOriginal
2024-08-30 15:41:02680browse

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 Tests

How to Use Mutable String 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 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.

Constructors of StringBuilder and StringBuffer Classes

The following are the main constructors of string builder and string buffer classes.

1. 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

2. StringBuilder Constructors

The following are String builder constructors:

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

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

Methods of string mutable classes are given below:

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 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.
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 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

Examples of mutable string in java are given below:

Example #1

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:

Mutable String in Java

Example #2

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:

Mutable String in Java

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Calculator in JavaNext article:Calculator in Java