String vs StringBuffer vs StringBuilder
Introduction
We will discuss on String vs StringBuffer vs StringBuilder here.
String, StringBuffer and StringBuilder.String
String is used to manipulate character strings that cannot be changed (read-only and immutable). If your string is not going to change use a String class.
- String is an immutable object, i.e., once created cannot be changed.
- String is a thread safe object as it is an immutable object, i.e., multiple threads cannot access String object simultaneously.
- The String object is stored in constant pool area of the String.
- Once assigned a value to String object cannot be changed.
The object s is stored in constant pool and its value can not be modified:
String s = "Hello, World!"; New Welcome to Hello World! String object is created in constant pool and referenced by the same s variable:
s = "Welcome to Hello World!" Hello, World! still exists in constant pool area of the String but we lost the reference.
StringBuffer
StringBuffer is used to represent characters that can be modified. If your string can change, and will be accessed from multiple threads, use a StringBuffer because StringBuffer is synchronous.
- It is a mutable object, i.e., we can change its value.
- The object created through
StringBufferis stored in heap. - It provides synchronized methods, hence it provides thread safety; so multiple threads can access
StringBufferobject simultaneously, though only one thread is allowed to access at a time. - As all methods in
StringBufferare synchronized, so it has drawback on performance and slower thanStringBuilderandStringobject. - The object created through
StringBuffercan be changed to String object usingtoString()method.
StringBuffer stringBuffer = new StringBuffer("Hello, World!"); StringBuilder
If your string can change (logic and operations in the construction of the string) and will only be accessed from a single thread, using a StringBuilder is good enough.
- It is a mutable object, i.e., we can change its value.
- The object created through
StringBuilderis stored in heap. - It does not provide any synchronized method, that means it does not provide thread safety; so multiple threads cannot access
StringBuilderobject simultaneously. - No method in
StringBuilderis synchronized, so it is faster thatStringBuffer. - The object created through
StringBuildercan be changed to String object usingtoString()method.
StringBuilder stringBuilder = new StringBuilder("Hello, World!"); Example
public class StringStringBufferStringBuilder {
public static void main(String[] args) {
String s = "Hello, World!";
System.out.println(s);
s = "Welcome to Hello World!";
System.out.println(s); // we lost reference to "Hello, World!"
System.out.println();
StringBuffer stringBuffer = new StringBuffer("Hello, World!");
System.out.println(stringBuffer);
System.out.println(stringBuffer.toString()); // convert to String object
stringBuffer = new StringBuffer("Welcome to Hello World!");
System.out.println(stringBuffer);
System.out.println();
StringBuilder stringBuilder = new StringBuilder("Hello, World!");
System.out.println(stringBuilder);
System.out.println(stringBuilder.toString()); // convert to String
// object
stringBuilder = new StringBuilder("Welcome to Hello World!");
System.out.println(stringBuilder);
}
} Output
By executing the above main class you will get below output:
Hello, World!
Welcome to Hello World!
Hello, World!
Hello, World!
Welcome to Hello World!
Hello, World!
Hello, World!
Welcome to Hello World! Thanks for reading.
No comments
Leave a comment