String – StringBuffer – StringBuilder in Java

Tram Ho


String is a basic class for string representation in Java. However in Java, String is immutable – i.e. cannot be modified. That is, once a String object is created, it cannot change its contents. If your program needs to make a lot of changes on a string, using String wastes resources and slows the program down.


In the example above:

  • String a = “String aa”, looking at it, we think we have successfully appended the letter “a” after “String a” to get the same result but no, we have just created a new memory area containing the value “String” aa” and again use the variable a to point to the location of the new value, and the old value that the variable a points to is “String a” which is not accessible to anyone, so it will be picked up by the garbage collector.


StringBuffer is a mutable class and is designed to support multi-threaded. When you perform any operation on a StringBuffer object, its contents will be modified directly without creating a new object. This makes StringBuffer used in multithreaded situations where multiple threads can access and modify the same StringBuffer object.

Note: StringBuffer is a synchronized class. It allows while one thread is using the StringBuffer object to perform a modification operation, other threads that need to modify the contents of that StringBuffer object must wait for the executing thread to finish its operation before its turn. . This helps to ensure that operations of threads on the same StringBuffer object are safe in a multithreaded situation.


In the above example, we use the StringBuffer class to concatenate the strings “Hello” and “World” together. However, the program runs on two different threads and performs operations on the common StringBuffer object. Therefore, we need to use StringBuffer to ensure that string operations are performed safely in multithreaded situations.

Note: The join() method of Thread in java is used to wait for a thread to finish before continuing to execute the next instructions in a program. When one thread calls another thread’s join() method, the calling thread is paused and does not execute further instructions until the awaited thread finishes.

In the above example: we use thread1.join() and thread2.join() statements to make the program wait for “thread1” and “thread2” to finish before terminating the program. Otherwise, the program will print the result as an empty String.


Like StringBuffer, StringBuilder is a mutable class and is designed to support single-threaded. Similar to StringBuffer, when any modification needs to be performed on a StringBuilder object, its contents are changed directly and a new object is not created. However, since there is no need to support multithreading, StringBuilder can be faster than StringBuffer.


In the above example, we use the StringBuilder class to concatenate the strings “Hello” and “World” together. Since the program runs on only one thread (single thread), we don’t need to worry about synchronization and can use StringBuilder to increase the performance of the program.

Share the news now

Source : Viblo