Saturday, June 14, 2014

the stupid old java question What is the difference between STRINGBUFFER and STRING?

Really who cares!

Well people who are concerned about performance - eg high message throughput - low latency trading apps.

we need to talk about immutable

Strings are immutabool !

using Strings fields creates a performance hit. although  the compiler will remove this for you and replace it with StringBuffer if it can.

its still faster to use StringBuffer - i have tested - well my colleague did it to prove me wrong in an argument.



StringBuffer/StringBuilder objects are mutable: StringBuffer/StringBuilder objects are mutable; we can make changes to the value stored in the object. What this effectively means is that string operations such as append would be more efficient if performed using StringBuffer/StringBuilder objects than String objects.

String str = “Be Happy With Your Salary.''
str += “Because Increments are a myth";
StringBuffer strbuf = new StringBuffer();
strbuf.append(str);
System.out.println(strbuf);

The Output of the code snippet would be: Be Happy With Your Salary. Because Increments are a myth.

SO TRUE!

No comments:

Post a Comment