Difference between String, StringBuffer and StringBuilder in java
What is a string in Java?
Strings are a collection of characters that are commonly used in Java programming. Strings are regarded as objects in the Java programming language.
“String” is a Java platform class that allows you to construct and handle strings.
In Java, strings are objects that represent a sequence of characters. They can be produced with either the String Literal or the NEW keyword. Strings in Java are immutable and stored in the UTF-16 encoding. When a new String is formed, it searches the JVM string pool for a String with the same value. If the value is the same, the reference is returned; otherwise, a String object is constructed and placed in the String pool.
To concatenate two strings, String uses the + operator, and this is done internally using the StringBuffer.
Creating String:
The normal way to create a string in Java is to simply write:-
String s = “Hello World”;
The compiler constructs a String object with the value "Hello world!" whenever it detects a string phrase in your code.
String objects cannot be made by using the new keyword and a function Object, just like any other object. The String class provides 11 constructors that allow you to set the string's initial value from a variety of sources, including an array of characters.
Another way to create a string is :-
String s = new String (“Hello World”);
Difference between String Buffer and String Builder
String Buffer and String Builder are two types of classes that can be used to manipulate strings. These are mutable objects that provide String manipulation operations such as sub string(), insert(), append(), and delete().
Following are the primary distinctions between StringBuffer and StringBuilder:
StringBuffer | StringBuilder |
When two or more threads are operating on the same String, StringBuffer is used. | StringBuilder is only used in a single-threaded atmosphere. |
StringBuffer has operations that are thread-safe and synchronized | StringBuilder operations are not thread-safe and are not also synchronized. |
When compared to StringBuilder, the performance of StringBuffer is slower. | When compared to StringBuffer, StringBuilder performs faster. |
Syntax: StringBuffer var = new StringBuffer(str); | Syntax: StringBuilder var = new StringBuilder(str); |
String vs StringBuilder vs StringBuffer in Java
The following two arguments determine the distinctions between String, StringBuffer, and StringBuilder:
- Mutability
- Performance
Mutability
When we compare the phrases above on the mutability factor, we get the following. Strings are immutable, although StringBuffer and StringBuilder are mutable, as previously mentioned. When using the String class, Strings cannot be altered; however, when using the StringBuffer and StringBuilder classes, Strings can change.
Example:
public static void StrConcat(String str1)
{ str1 = str1 + "World"; }
public static void StrBufConcat(StringBuffer str2)
{ public class example {
str2.append("World");
}
public static void StrBuildConcat(StringBuilder str3) {
str3.append("World");
} public static void main(String[] args)
{ String str1 = "Hello!";
StrConcat(str1);
System.out.println("The final String is - " + str1);
StringBuffer str2 = new StringBuffer("Hello!");
StrBufConcat(str2);
System.out.println("The final String is - " + str2);
StringBuilder str3 = new StringBuilder("Hello!");
StrBuildConcat(str3);
System.out.println("The final String is -" + str3);
}
}
Output:
The final string is – Hello!
The final string is – Hello! World
The final string is – Hello! World
StrConcat, StrBufConcat, and StrBuildConcat are the three functions in the code above.
I gave a string -> "Hello!" to the first function, and then used the + operator to perform concatenation [str1 = str1 + "World"]. If you look at the output, you'll notice that the String provided to main() hasn't changed because the String is immutable. Because str1 in main() refers to "World," while str1 in StrConcat() refers to a different object, this is the case.
However, the result of the other two functions, StrBufConcat and StrBuildConcat, is identical since these objects are changeable. I used the add() function to concatenate the Strings in the second and third functions.
Performance
Due to the lack of synchronization, StringBuffer is slower than StringBuilder. This is due to the fact that no additional overhead is required and the procedure is not slowed.
Parameter | String | StringBuffer | StringBuilder |
Storage | String Pool | It has Heap | It also has a Heap |
Mutability | Immutable(unchangeable) | Mutable(changeable) | Mutable(changeable) |
Thread Safe | A string is not used in a threaded environment | StringBuffer is used in a multi-threaded environment | StringBuilder is used in a single-threaded environment |
Performance | Strings are slow | StringBuffer is slower than StringBuilder but faster than String | StringBuilder is faster than StringBuffer |
Syntax | String var =“Hello”; String var=new String(“Hello”); | StringBuffer var = new StringBuffer("Hello"); | StringBuilder var = new StringBuilder("Hello"); |