+ 2
Java strings
What is the difference between string,string builder and string buffer?
7 Réponses
+ 4
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
+ 2
Atul [Inactive] thank you
+ 1
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
+ 1
Ciro Pellegrino thank you so much
+ 1
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
https://code.sololearn.com/c4GLPURfx1FF/?ref=app
https://code.sololearn.com/cIQ86W7lMObM/?ref=app
Incase if you need examples
0
Ciro Pellegrino thank you
Can u also explain what thread safe means?