String vs StringBuilder vs StringBuffer
String is an integral part of Java programming. String is nothing but a sequence of characters. It is one of the most widely used classes in Java.
Create a String
There are two methods which are usually followed to create a string in java.
- Using String Literal
- Using the new keyword
These string objects in java are immutable which means, they are constant and cannot be changed once created. But what if, you want to change the values possessed by a string variable for various String operations, like concatenation, substring.
StringBuffer and StringBuilder
Since String is immutable in Java, whenever we perform any kind of String manipulation, it
generates a new string discarding the old string for garbage collection. Let me tell you if
you don`t know already that these are heavy operations we are talking about and it generates
a lot of garbage in bulk.
In order to avoid this problem, Java has provided StringBuffer and
StringBuilder class
that take care of String manipulations. Even the String concatenate operator uses
StringBuffer or StringBuilder class internally. These are mutable objects
in Java and they
offer various methods for String manipulation. The various methods include:
- append()
- insert()
- delete()
- substring()
To understand the difference between String, StringBuffer and StringBuilder let us look at an example.
Example
Output
Let us look at the output of the above example code.
In the first scenario, since string is immutable, the string passed from main() is not
changed.
In the second scenario, with StringBuilder class when we pass a string
“Hello” and
perform s2.append(“World”), it changes the actual value of the string in
main() to
“HelloWorld”.
Similar to the second scenario, in the third case, with StringBuffer class when
we pass
a string “Hello” and perform s2.append(“World”), it changes the actual
value of the string
in main() to “HelloWorld”.
The second and third case looks almost same, but they have one difference that
StringBuffer is thread safe and multiple threads can use it without any issue. We
can also
point out the difference between these two classes in another manner which is,
StringBuffer
is mutable and synchronized, whereas StringBuilder is mutable but not synchronized
by
default.
Synchronization and Thread - Safety
In the above text we talked about synchronization and thread-safety, but
what do these term
actually mean? Let us talk about threads first.
A thread in Java is the path which is followed when executing a program. Each
and every
Java program has at least one thread which is known as the main thread. It is created by the
Java Virtual Machine (JVM) at the beginning of a program when the main() method is
invoked.
Java is a multi-threaded application that allows multiple threads execution at any
particular time. It`s nature of multi – threaded environment creates a concept of thread –
safety which implies the correct behaviour of a method, variable or field in a program when
multiple simultaneous threads are using a resource. Now let us look at synchronization.
When we say something is synchronized in a Java program, it means that multiple
threads
can access, and modify it without any problem or side effect. StringBuffer is
synchronized,
so you can use it with multiple threads without any problem.
Summary
If a string is going to remain constant throughout a program, then use the String
class.
If a string can change and will be accessed by only one single thread, then use the
StringBuilder class. And if it is being accessed by multiple string then use
StringBuffer
class as it provides thread – safety.
It should be noted that, you should not use StringBuffer unnecessarily, which
means,
don't use it if only one thread is modifying and accessing it because it has lot of locking
and unlocking code for synchronization which will unnecessarily take up CPU time.