+ 2

Java strings

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

8th Jul 2021, 8:01 AM
alagammai uma
alagammai uma - avatar
7 Antworten
+ 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
8th Jul 2021, 1:36 PM
Ciro Pellegrino
Ciro Pellegrino - avatar
+ 2
Atul [Inactive] thank you
9th Jul 2021, 2:29 AM
alagammai uma
alagammai uma - avatar
+ 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
8th Jul 2021, 6:00 PM
Ciro Pellegrino
Ciro Pellegrino - avatar
+ 1
Ciro Pellegrino thank you so much
8th Jul 2021, 6:02 PM
alagammai uma
alagammai uma - avatar
+ 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
8th Jul 2021, 6:12 PM
Ciro Pellegrino
Ciro Pellegrino - avatar
9th Jul 2021, 2:07 AM
Atul [Inactive]
0
Ciro Pellegrino thank you Can u also explain what thread safe means?
8th Jul 2021, 2:23 PM
alagammai uma
alagammai uma - avatar