In this post we’ll go through the differences among the String, StringBuffer and StringBuilder classes in Java which is a very important String interview question too.
String in Java has many characteristics like being immutable, thread safety, declared as final that sets it apart. But, being immutable also means that any modification to String object will result in a creation of new string even if you use operations like converting to lowercase, uppercase. Thus concatenation of many strings will result in creation of many objects making it less memory efficient in that case.
Enters StringBuffer in Java which is mutable and has an append() method which can append to the original StringBuffer and doesn’t result in creation of many objects. StringBuffer is also thread safe but that thread safety comes from the fact that many of its methods are synchronized. This synchronization makes it a little slow.
Enters StringBuilder class in Java which provides the same methods as StringBuffer but it is not thread safe. Which makes it faster, as it performs no synchronization.
String Vs StringBuffer Vs StringBuilder in Java
Let's see the differences among the String, StringBuffer and StringBuilder in Java. Differences are listed in tabular form where a feature is listed and how specific class behave with respect to that feature is explained.
String | StringBuffer | StringBuilder | |
---|---|---|---|
Creation | Apart from creating strings using new operator, String object can also be created like primitive data types are created as example String str = "test"; When String is created this way it is stored in a common string pool. | StringBuffer object can only be created using new operator. StringBuffer object is stored in Heap. | StringBuilder object can only be created using new operator. StringBuilder object is stored in Heap. |
Modifiability | Once you create a String object the content of that string cannot be modified i.e. String objects are immutable. Here being immutable means whenever you perform any operation on string which alters its content a new string object is created which contains the modified string. Original string is left as it is. | StringBuffer is a mutable(modifiable) sequence of characters. Thus in case of StringBuffer length and content of the sequence can be changed through certain method calls. | StringBuilder is a mutable(modifiable) sequence of characters. Thus in case of StringBuilder length and content of the sequence can be changed through certain method calls. |
Appending | If you have to append a lot of strings it will take more memory. Since string is immutable so concatenation of strings will result in creation of a lot of intermediary strings. | StringBuffer class is mutable so appending to it using append() method will not result in creation of intermediary objects. That way less memory is used. | StringBuilder class is mutable so appending to it using append() method will not result in creation of intermediary objects. That way less memory is used. |
Thread-safety | As string objects are immutable so they are thread safe. | StringBuffer is thread safe so String buffers are safe for use by multiple threads. Each method in StringBuffer is synchronized making it thread safe. | StringBuilder is not thread-safe. |
Performance | String class is fast. | Most of the methods in StringBuffer class are synchronized which makes it a little slow. | If you don't have to bother about thread safety then use StringBuilder class as it supports all of the same operations as StringBuffer but it is faster, as it performs no synchronization. |
That's all for this topic String Vs StringBuffer Vs StringBuilder in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!
>>>Return to Java Basics Tutorial Page
Related Topics
You may also like-
No comments:
Post a Comment