4 ms·
You shouldn't use StringBuffer. The reason it is slow is because it is "thread safe". 99% of the time, you don't really want that (especially in cases where y
by cogman10 6y ago
You shouldn't use StringBuffer.
The reason it is slow is because it is "thread safe". 99% of the time, you don't really want that (especially in cases where you know how big the string will be at the end of everything).
StringBuilder should be preferred for pretty much everything.
- hyperman1 6y agoWhile I agree with the conclusion to strongly prefer StringBuilder, in most cases the difference is zero: The JVM will prove no other thread can access the object, so it remooves the locks
- cogman10 6y agoI'm not sure if I agree about most cases. It is fairly trivial to come up with code that goes down a sad path (you just need to defeat the Escape analysis). That happens all the time. The JVM's Escape analysis isn't perfect.