Last Updated: 06 August, 2023
StringBuilder is mutable, non-synchronized, and a final class in Java. It was introduced in the JDK 1.5 version and placed in the java.lang package.
StringBuilder is used to create a mutable String object, which means we can modify the string without creating a new object. StringBuilder provides no guarantee of thread safety as it is non-synchronized at a time when multiple threads can access the StringBuilder object.
StringBuilder class description
public final class StringBuilder extends Object
implements Serializable, CharSequence
StringBuilder is similar to StringBuffer except for one major difference: StringBuilder is non-synchronized, whereas StringBuffer is Synchronized. For this reason, StringBuilder is faster than StringBuffer in terms of execution speed and performance.
Example: A StringBuilder class implementing in Java
Output
Welcome in StringBuilder Class
Constructor | Description |
---|---|
StringBuilder() | Constructs a string builder with no characters in it and an initial capacity of 16 characters. |
StringBuilder(int capacity) | Constructs a string builder with no characters in it and an initial capacity specified by the capacity argument. |
StringBuilder(String str) | Constructs a string builder initialized to the contents of the specified string. The initial capacity of the string builder is 16 plus the length of the string argument. |
StringBuilder(CharSequence seq) | Constructs a string builder that contains the same characters as the specified CharSequence. The initial capacity of the string builder is 16 plus the length of the CharSequence argument. |
Syntax: public int length()
The length() method returns the total length of StringBuilder object. This method always returns integer value as an output. If StringBuilder object is empty then returns 0.
Output
Length of StringBuilder: 16
Syntax: public StringBuilder append(data-type obj)
Appends the specified data to the StringBuilder object, append() method is a overloaded method in StringBuilder.
Output
StringBuilder initial value: java
StringBuilder updated value: javaby
StringBuilder updated value: javabytechie
StringBuilder updated value: javabytechie.com
Syntax: public int capacity()
Returns the current capacity. The capacity is the amount of storage available for newly inserted characters, beyond which an allocation will occur.
Output
StringBuilder Capacity: 32
Syntax: public char charAt(int index)
Returns the char value in this sequence at the specified index. The index start from 0 same as array. If given index value beyound the range then will get IndexOutOfBoundsException.
Output
StringBuilder value: javabytechie.com
6th index character: t
Syntax: int compareTo(StringBuilder another)
Compares two StringBuilder instances lexicographically. This method returns 0 if both StringBuilder objects are same; a negative integer if this StringBuilder is lexicographically less than the StringBuilder argument; or a positive integer if this StringBuilderis lexicographically greater than the StringBuilder argument.
This method is added in Java 11 version.
Output
StringBuilder1 value: => javabytechie
StringBuilder2 value: => javabytechie
0
StringBuilder2 value after append: => javabytechie.com
-4
Syntax: public StringBuilder delete(int start, int end)
Removes the characters in a substring of this sequence. The substring begins at the specified start and extends to the character at index end - 1 or to the end of the sequence if no such character exists.
Output
StringBuilder value: javabytechie.com
After delete: javabechie.com
Syntax: public StringBuilder deleteCharAt(int index)
Removes the char at the specified position in this sequence. This sequence is shortened by one char.
Output
StringBuilder value: javabytechie.com
After delete:javabyechie.com
Syntax: public int indexOf(String str)
Returns the index within this string of the first occurrence of the specified substring.
Output
StringBuilder value: javabytechie.com
Index value: 6
Syntax: public int lastIndexOf(String str)
Returns the index within this string of the rightmost occurrence of the specified substring.
Output
StringBuilder value: javabytechie.com
Last index value: 4
Syntax: StringBuilder replace(int start, int end, String str)
Replaces the characters in a substring of this sequencewith characters in the specified String.
Output
StringBuilder value: javabytechie
After replace: javaOnlineTutorial
Syntax: public StringBuilder reverse()
Replace this character sequence with the reverse of the sequence order.
Output
StringBuilder value: javabytechie.com
Reversed value: moc.eihcetybavaj
Syntax: public String substring(int start)
Returns a new String that contains a subsequence of characters currently contained in this character sequence. The substring begins at the specified index and extends to the end of this sequence.
Output
StringBuilder value: javabytechie.com
Substring value: techie
That's all guys, hope this Java article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com
What is the difference between StringBuffer and StringBuilder in Java?
Ans. StringBuffer and StringBuilder classes are used to represent a sequence of characters in Java, and both classes are by nature final and mutable objects. There are the following differences between these two classes:
StringBuffer | StringBuilder |
---|---|
The StringBuffer object is thread-safe and synchronized. Multiple threads cannot call the methods of StringBuffer simultaneously. | The StringBuilder object is not synchronized and not thread-safe. Multiple threads can call the methods of StringBuilder simultaneously. |
StringBuffer is suitable for multi-threaded environments. | StringBuilder is suitable for single-threaded environments. |
StringBuffer performance is slower as compared to StringBuilder. | StringBuilder performance is faster as compared to StringBuffer. |
StringBuffer has been available since the JDK 1.0 version. | StringBuilder has been available since JDK Version 5. |
How to convert StringBuffer to StringBuilder in Java?
Ans. To convert StringBuffer to StringBuiler, first we need to convert StringBuffer to a String object by using the toString() method, and then we will create StringBuilder and pass the String object into the constructor.
Let's see an example:
public class ConvertBufferToBuilder { public static void main(String[] args) { StringBuffer buffer = new StringBuffer("JavaByTechie"); // STEP: 1 // Conversion from StringBuffer object to the String object String str = buffer.toString(); // STEP: 2 // Creating a StringBuilder and passing String object (str) into the constructor StringBuilder builder = new StringBuilder(str); System.out.println("StringBuilder value: " + builder); } }
Output
How to convert StringBuilder to StringBuffer in Java?
Ans. Same as StringBuffer to StringBuiler conversion process, first we need to convert StringBuilder to a String object by using the toString() method, and then we will create StringBuffer and pass the String object into the constructor.
Let's see an example:
public class ConvertBuilderToBuffer { public static void main(String[] args) { StringBuilder builder = new StringBuilder("JavaByTechie"); // STEP: 1 // Conversion from StringBuilder object to the String object String str = builder.toString(); // STEP: 2 // Creating a StringBuffer and passing String object (str) into the constructor StringBuffer buffer = new StringBuffer(str); System.out.println("StringBuffer value: " + buffer); } }
Output
How to convert a String into StringBuilder and vice versa?
Ans.
public class StringToStringBuilderAndViceVersa { public static void main(String[] args) { StringBuilder sb = new StringBuilder(); String str1 = new String("String is an object in Java."); String str2 = "String is a sequance of characters"; // Converting String into StringBuilder sb.append(str1); sb.append(str2); // Converting StringBuilder into String String str = sb.toString(); System.out.println("String value: " + str); System.out.println("StringBuilder value: " + sb); } }
Output
String value: String is an object in Java.String is a sequance of characters
StringBuilder value: String is an object in Java.String is a sequance of characters