What is the difference between StringBuilder and StringBuffer in Java?
Hi everyone! I'm learning Java and writing a small server module for processing logs. I need to build strings from many fragments, and I know that concatenation using "+" in a loop is bad because it creates many unnecessary String objects.
I started using StringBuffer, but my team lead said I should switch to StringBuilder. I didn't quite understand why. Online sources say both classes do the same thing: allow you to modify a string without creating new objects. So why are there two classes? And which one should I choose?
Here's an example of my code:
```java
public class LogBuilder {
public static void main(String[] args) {
StringBuffer log = new StringBuffer();
for (int i = 0; i < 1000; i++) {
log.append("Event #").append(i).append(": ");
log.ap
No answers yet.
Log in to leave an answer
Blogs