34 ms·
I completely agree on the buffer size statement, but at the same time, find it interesting that you would use StringBuffer enough to make that comment. I person
by jrpelkonen 6y ago
I completely agree on the buffer size statement, but at the same time, find it interesting that you would use StringBuffer enough to make that comment. I personally haven’t found any compelling use cases for it since Java 1.5 introduced StringBuilder. Would you like to share some ways you find StringBuffer useful?
- cogman10 6y agoYou 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.