In this lecture, we'll find the answer to the question 43.
What is the difference between string and string builder?
String is a type used for representing textual data.
String Builder is a utility class created for optimal concatenation of strings.
We all know strings.
We use them all the time to represent some text.
What some people don't know is that all strings are immutable, which means once created, they can't
be quantified.
If you haven't watched the previous lecture, you may be a bit surprised.
You probably mutated strings plenty of times by now.
For example, let's try to modify this string.
So what's the big deal?
We just modified a string.
First Lieutenant, the value from ABC to the IMF, which obviously is a modification, and then we are
the retreat which resulted in the final value of the TFG.
So what's the fuss about the immutability?
Well, the thing is, we actually didn't modify the ABC string.
We created our brand new DFA string, and we simply pointed the reference stored in some string variable
to this new string.
A similar thing happened in the next line.
We created a new string by concatenating the F with G and then pointed the reference to this new string
at some point in time.
The garbage collector will see those old strings as objects to whom no reference points and will remove
them from memory.
So we now know that when we modify a value of string, the following things need to happen a new object
needs to be created, which involves allocating memory for it.
The variable that was pointing to the original string must be pointed to the new string at some point.
The garbage collector must clean out the old string that's relatively out of work.
In many cases, it's OK, and we don't notice any performance impact when we add Mr. to the John Smith
string.
But I need to build strings gradually from parts.
It's pretty common.
For example, imagine your application is done holding some data from the web and it does it in chunks
as the data is pretty large.
You need to create some kind of extract from this data for each task, right?
And it can be thousands of them.
You need to build some pretty complex string and then append district the string representing the final
result.
It can involve millions of concatenation operations as the process continues.
The final result is growing.
At some point it can be a huge string.
And still, every concatenation keeps copying it to a new, huge part of memory, adding some tiny part
and then replacing the reference stored in the original variable.
Not to mention that behind the scenes, the garbage collector is struggling to clean up all those large
but unused strings from memory.
And for such use cases, the string builder class has been created.
The string builder can add or remove pieces from the final result.
But we folded these laborious copying of the old string, adding apart and removing the old string string
builder object maintains a buffer to accommodate expansions to this string.
New data is appended to the buffer if there is any space in it left.
Otherwise, a new Rajo buffer is allocated.
Data from the original buffer is copied to the new buffer, and the new data is then appended.
As you can see, the only scenario when the entire result is being copied is one.
The buffer needs to be enlarged.
All right, let's see a simple program that will measure the performance of string and string builder.
Those two methods seem to build a string of letters, a of the length given in the parameter.
The only difference is that this one is using a string and this one the string builder class.
Let's see how they will handle 100000 iterations.
Well, string builder gives the result over five thousand five hundred times faster than string.
I hope I convinced you that when you implement some process of incremental building of things, the
string builder should be the cloth that you use.
Of course, for simple uses like concatenating a couple of strings, using the string builder is an
overkill and it only complicates the code.
And if you wanted to have at least a tiny performance boost, I must disappoint you.
This drink is not built incrementally, but is composed in a single instruction plain old string addition
actually works faster if you try to do the same with the string builder.
This is how it would look like.
Actually, this code is not only much prettier and simpler than this one, but it actually works faster.
The performance of this code is better because behind the scenes, it gets translated into this.
Which is actually quite efficient.
Remember, this can only be done if concatenation happens in a single instruction.
So it would not work if, for example, we used a loop to build a string from pieces.
All right.
The most important thing that we need to remember from this lecture is to use string builder one incrementally
building cloud strings as it gives much better performance than using a simple string.
During the interview, you can be asked What does it mean that string is immutable?
It means that one string is created.
It can't be modified when we modify a string.
Actually, a brand new string is created and the variable that was storing it, we simply hold a reference
to this new object.
Oh, right.
That's it in this video and in the subject of strings.
Thanks for watching and see you in the next one.