Full Stack Web Development Internship Program
- 5k Enrolled Learners
- Weekend/Weekday
- Live Class
A string is an object which represents a sequence of characters. Strings are one of the most popular classes used while programming in Java by developers. But, since Strings are immutable, Java came up with two utility classes: StringBuilder and StringBuffer to make the String manipulations easy. So in this article, I will brief you on the differences between String, StringBuffer and StringBuilder.
The following will be the topics covered:
Strings in Java are objects used to represent a sequence of character. They can be either created using the String Literal or by using the NEW keyword. Strings are immutable in Java are represented in the UTF-16 format. When a new String is created, it looks for the String with the same value in the JVM string pool. If it finds a same value, then it returns the reference else it created a String object and places that object in the String pool.
Apart from this, String uses the + operator to concatenate two strings and internally used the StringBuffer to perform this action. If you wish to know more about Strings in Java and its methods, you can refer to the article – Java Strings.
Since Strings are immutable in Java, whenever String manipulations are performed, automatically a new String would be generated by discarding the older one. But, don’t you think, this is cumbersome when we have large applications?
Well, to avoid garbage in heap, Java came up with StringBuffer and StringBuilder. So next in this article let us understand what are StringBuffer and StringBuilder.
StringBuffer and StringBuilder are classes used for String manipulation. These are mutable objects, which provide methods such as substring(), insert(), append(), delete() for String manipulation.
The main differences between StringBuffer and StringBuilder are as follows:
StringBuffer | StringBuilder |
StringBuffer operations are thread-safe and synchronized | StringBuilder operations are not thread-safe are not-synchronized. |
StringBuffer is to used when multiple threads are working on the same String | StringBuilder is used in a single-threaded environment. |
StringBuffer performance is slower when compared to StringBuilder | StringBuilder performance is faster when compared to StringBuffer |
Syntax: StringBuffer var = new StringBuffer(str); | Syntax: StringBuilder var = new StringBuilder(str); |
Next, in this article, let us see the differences between String, StringBuffer and StringBuilder.
The differences between String, StringBuffer, and StringBuilder are based on the following two parameters:
Let us look into each one of them one by one.
Well, when we compare the above terms, on the factor Mutability. Then, as mentioned before, Strings are immutable while StringBuffer and StringBuilder are mutable. So, Strings cannot be changed when you use the String class; whereas Strings can change if you use the StringBuffer and StringBuilder class.
Consider the following code snippet:
package edureka; public class example { public static void StrConcat(String str1) { str1 = str1 + "Edureka"; } public static void StrBufConcat(StringBuffer str2) { str2.append("Edureka"); } public static void StrBuildConcat(StringBuilder str3) { str3.append("Edureka"); } 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); } }
In the above code, I have three functions: StrConcat, StrBufConcat, and StrBuildConcat.
In the first function, I have passed a string -> “Hello!” and then to perform concatenation, I have used the + operator as [str1 = str1 + “Edureka”]. Now, if you observe the output, the String passed in the main() did not change, as String is immutable. This is because str1 in main() references to “Edureka” and str1 in StrConcat() references to a different object.
But, if you look at the other two functions: StrBufConcat, and StrBuildConcat; the output is the same as these objects are mutable. In the second and the third function, I have used the append() function to concatenate the Strings.
StringBuilder is faster than StringBuffer as it offers no synchronization. This is because no extra overhead needs to be added to the system and also does not slows down the processing.
If I have to summarize the differences between String, StringBuffer and StringBuilder, then refer to the below table:
Parameter | String | StringBuffer | StringBuilder |
Storage | String Pool | Heap | Heap |
Mutability | Immutable | Mutable | Mutable |
Thread Safe | Not used in a threaded environment | Used in a multi-threaded environment | Used in a single-threaded environment |
Performance | Slow | Slower than StringBuilder but faster than String | Faster than StringBuffer |
Syntax | String var = “Edureka”;
| StringBuffer var = new StringBuffer("Edureka"); | StringBuilder var = new StringBuilder("Edureka"); |
With this, we come to an end to this article. I hope you found this article informative.
Now that you have understood basics of Java, check out the Java training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Edureka’s Java J2EE and SOA training and certification course is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.
Got a question for us? Please mention it in the comments section of this article on “String vs StringBuilder vs StringBuffer in Java” and we will get back to you as soon as possible.
Course Name | Date | |
---|---|---|
Java Certification Training Course | Class Starts on 28th January,2023 28th January SAT&SUN (Weekend Batch) | View Details |
Java Certification Training Course | Class Starts on 25th February,2023 25th February SAT&SUN (Weekend Batch) | View Details |
edureka.co