While learning Strings in java we come across various classes like String, StringBuilder and StringBuffer which are not available in other languages. In this article, we will learn the basic difference between these classes and where to use which class.
So, Let's start
What is a String?
A String is a sequence of characters. But unlike some other languages that implement string as a character array.Java implements string as an object of type String. Now, when you create a String, you are creating a string that cannot be changed ( i.e Immutable). So, whenever you change a string, a new String object is created that contains the changes.
import java.util.*;
public class Main {
public static void main(String[] args) {
String name = "Jhon"; // Initialization of String
}
}
Some Common Operations are performed on Strings.
Concatenation: Joining two or more strings using the
+
operator.Length: Finding the number of characters present in the string using the
length()
method.Substring: Extracting the portion of a string using the
substring()
method.Comparison: String can be compared using the
equals()
method or thecompareTo()
method.Searching: The position of the substring within a string can be found using the
indexOf()
method.
What are StringBuffer and StringBuilder ?
The StringBuffer and StringBuilder classes are defined in java.lang package. So, they are available to all programs automatically. As the string is Immutable in java, we use StringBuffer and StringBuilder Classes to create mutable strings in java. These classes provide several methods to append, insert, delete, and replace characters in the sequence.
String name = "Jhon"; // Initialization of String
// You can create StringBuffer in following two ways.
StringBuffer name1 = new StringBuffer(name); // Converting string to StringBuffer.
StringBuilder name2 = new StringBuilder(name);// Converting string to StringBuilder.
Some Common Operations are performed on StringBuilder and StringBuffer.
Append: Appends the specified string to the end of the sequence using
append(String str)
.StringBuffer name = new StringBuffer("Jhon"); name.append(" Wick"); System.out.println(name.toString()); // Output: Jhon Wick
Insert: Inserts the specified string at the specified position in the sequence using
insert(int index, String str).
StringBuffer name = new StringBuffer("Jhon"); name.insert(5, " Wick"); System.out.println(name.toString()); // Output: Jhon Wick
Delete: Deletes the characters in the sequence between specified start and end positions using
delete(int start, int end)
.StringBuffer name = new StringBuffer("Jhon Wick"); name.delete(4, 9); System.out.println(name.toString()); // Output: Jhon
Replace: Replaces the characters in the sequence between the specified start and end positions with the specified string using
replace(int start, int end, String str)
.StringBuffer name = new StringBuffer("Jhon"); name.replace(5, 9 , ""); System.out.println(name.toString()); // Output: Jhon
Reverse: Reverse the order of the characters in the sequence using
reverse()
.StringBuffer name = new StringBuffer("Jhon"); name.reverse(); System.out.println(name.toString()); // Output: kciW nohJ
Difference between StringBuilder and StringBuffer.
StringBuffer is designed to be thread-safe, which means it can be accessed by multiple threads simultaneously without causing any errors or data corruption but at the cost of performance. On the other hand, StringBuilder is similar to StringBuffer but does not provide thread safety and synchronization. So, if you are in a single-threaded environment or don’t care about thread safety, you should use StringBuilder. Otherwise, use StringBuffer for thread-safe operations. Performance wise String builder performs better than StringBuffer even in the case of a single-threaded environment.
Which one to use 🤔?
If you don't want to perform major changes in the String use a simple String Object.
If you want to perform multiple operations on a string that can only be accessed from a single thread, using a StringBuilder is good enough.
If you want to perform multiple operations on a string in a multithreaded environment use StringBuffer.
Hopefully, I was able to make my point.
In case I have missed something or you have some advice or suggestions do let me know in the comment section.
You can also reach out to me Here.
Happy Coding ✌️