Java strings | Sololearn: Learn to code for FREE!


Java strings

What is the difference between string,string builder and string buffer?

7/8/2021 8:01:30 AM

alagammai uma

7 Answers

New Answer


a String object is immutable and changes cause the generation of a new String object. StringBuffer and StringBuilder are like character arrays and can be changed without a new object being produced. But while StringBuffer is thread-safe and synchronized resulting slower, StringBuilder is not thread-safe, it is not synchronized but it is faster. therefore StringBuilder is ideal in non-multi threaded environments, otherwise it is necessary to use StringBuffer


Atul [Inactive] thank you


a thread you can think of as a program in your program. while your program is running it can generate threads (Thread class or Runnable interface) that are executed. if the system allows it, the execution of several threads can take place in parallel. but threads can also interact with each other, exchange data and need to be synchronized. if you use threads and need mutable strings then you will use the StringBuffer class designed to work in threads safely. otherwise you will use StringBuilder because it is faster


Ciro Pellegrino thank you so much


let's say two threads access the same StringBuffer. modify access must be synchronized so that each completes its modification without the other thread changing the object or accessing it prematurely. the methods must therefore be synchronized, so whoever commits the resource puts the others on hold. all this does not happen in the StringBuilder

+1 Incase if you need examples


Ciro Pellegrino thank you Can u also explain what thread safe means?