StringBuilder, StringBuffer and JVM flags for Strings

All

When dealing with Strings, which are of course immutable objects in Java, it may be easier to use a StringBuilder or StringBuffer implementation – as suggested by Geekforgeeks

StringBuffer vs StringBuilder

StringBuffer has a slight better performance than StringBuilder and is able to deal with multiple thread accessing. But is good to know it is there.

 StringBuilder stringBuilderExample = new StringBuilder("Example");
 stringBuilderExample.append("of String Builder");

 StringBuffer stringBuffer = new StringBuffer("Example");
 stringBuffer.append(" of String Buffer");

But factually, comparing both of them in a small benchmark, the performance is the same pretty much, but for a small benchmark, stringBuffer uses a little bit less memory.

real	0m0.048s <----------------------------- 0.048s (from 0.035s up to 0.055s)
user	0m0.044s
sys	0m0.009s

String JVM flags

There are some JVM flags that come in hand when dealing with strings as well:

JVM flagResult
UseStringCache
UseCompressedStrings
OptimizeStringConcat

Special thanks

Special thanks to Francesco Marchioni and his amazing blog, the very famous mastertheboss blog. I’m his fan and pretty much have his whole collection of books on my table, DataGrid, Quarkus, EAP, JBoss.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s