Hello, Jim.
In this lesson, we are going to learn how to work with swing builder and string buffer classes.
Also, I'm going to explain why we might need them.
We're going to start from understanding what these classes are and why do we need them and want to understand
the problems that these classes are supposed to solve.
We're going to have practical example where I will explain in detail why you should never forget about
string builder and string the buffer in this lesson.
I'm going to explain the difference between these two classes and what is even more important.
I will teach you when to use these classes and how to use them.
I will show you example of string builder and string the bar for usage and also will char sequence interface.
Let's start let's try to understand first what a string builder class in the documentation.
This class is described as mutable sequence of characters by this moment.
Now, of course, you already know that objects of string class are immutable.
In short, there's a class designed in the way that you can't change the state of a regional object
after it was instantiated.
All assets don't modify state of the original string or objects that we created.
The class itself is final and you can't override it to change the logic of methods.
That means that each time you modify string in your program, you actually create a new object of the
string.
Yes, you might lose the reference to the string object in case new string object is assigned to the
regional variable.
And yes, in the case there is no strong references to the object, garbage collector will clean it
up.
I'm going to show this case in the example later today.
But the only question that I want to ask you sounds like this.
Do we need to rely on garbage collector and make it work?
The answer is no one garbage collector starts its work.
It is also some computation time that potentially might be used by US operations.
Instead of cleaning up objects without strong references.
And by the way, it is important for you to know and remember the difference between different types
of references.
In case you want to learn more about this topic, make sure you watched my lesson about references where
I explained different types of references, but still how to solve a problem of constant creation of
string object.
If I'm modifying string so the situation might be completely different is a process a lot of strings
and you need all of them in one string or you prepare string before writing it to the file.
It doesn't matter.
But in some hypothetical case, instead of creation, millions of string objects and heap memory, it
is recommended to create one object of string, build it up and use it.
Does it make sense to understand the motivation of using an object of this type?
Great.
Let's now understand what a string buffer class is.
It would be easy to explain what is this class about because it is almost the same as string builder
class.
They have similar methods, similar constructors.
Literally almost everything is similar.
The only difference is that string, the buffer is a safe class and objects of this type might be used
in an execution without impact on data consistency.
What a lot of training is discussed in the separate section of the course.
But still, I would like to remind to all students about multithreaded one more time in in case of Perou
execution of multiple threads.
When different threads update the state of the string buffer simultaneously or read its data, only
one thread is allowed to execute these operations with potential mistakes that may happen because of
an axis.
That's why everything what I'm going to share with you about String Builder is absolutely applicable
to the string buffer in Montessori environment.
The rule of thumb is to use three builder whenever is possible because it has better performance.
This is just consequences of not having synchronization from multiple threads inside string builder,
whereas all mascot's in string buffer are synchronized and this takes additional overhead during the
execution.
You should be all right with String Builder.
In most cases, I believe that you already understood on the high level why we need these classes.
It is time to dive deeper in details and understand how to work with objects of this type.
And let's start from explaining the hypothetical problem.
I have file with stacks.
This is lyrics of the song that I listened while.
Thinking about the structure of the lesson, the song is called Never Coming Home By Staying Pretty
Cool one, by the way.
And according to business logic, I need to be able to read text from files and after that work with
the content of the file as a whole.
For example, text analysis or text statistics is needed.
Probably you want to implement programs that would analyze thousands of songs in order.
You could find the best rhymes any way you can be in any of other situations that would make it to collect
all streams in one string object.
I open my example now that is called string build a demo I have mastered that reads all lines from file
into one string object and after that I bring the strings to console.
Let's review this method.
I create the reference of the same time as it is pointing out to the object that is stored in St. Paul.
If you don't remember what a string will please refer to as a lesson about string objects and string
CPU in my Java course.
In the next line I read all lines from file into the list of strings.
In case you're interested how I did this, please refer to the section about input output streams in
my Java course.
After that I iterate and over each line and I concatenate each line with my result string and adding
new one character at the end of each line.
What is actually happening here?
I create a new string object after each concatenation and this string reference pointing out to the
new object in memory.
Each time after concatenation you are going to have the object that contains the first line.
Only the second object would be the first line.
And Newline character is a shared object, would be the first line newline character and the second
line and so on.
Why do you need so many duplication and so many string objects?
And that is not a big harm in process in one song.
But believe me, you are going to work with files much bigger than we have in our example today.
And this is going to become a problem.
What would be the solution here?
The solution here is to use string build object.
Let's look at another method that also read all lines from the file, but do it with help of string
builder object.
I create object of string, build a type with the default constructor.
After that I am doing the same thing.
I'm bringing the lines into the list.
Object in foreach loop.
I call append massata patterns new line and newline character.
I need to return a string object from the method so I call to string methods.
It creates string object based on my string builder.
That's it.
To prove you that concatenation of strings with string builder is more efficient, I evaluate the time
for both of these separations.
I just take amount of milliseconds before the first method invocation and after that I calculate Delta
in milliseconds by the doctors the time that I already have from current time after method execution.
Following this approach, I can estimate how long does it take to execute code?
That is between two points.
And I did similar measurement in the second example for String Builder.
After that I bring two values to console in order we could have a chance to compare these two values.
Let's run the program so you can see the difference in performance with one song of string is approximately
ten times more restrained.
Object concatenation.
Imagine what the difference would be was a gigabyte files.
It is crazy.
That's why it is important to use string builder during the processing of big number of strings.
Basically, we have just removed probably the most popular example of string builder and string bar
for usage.
But let me also share with you a little bit more details about class structure and other methods.
I open the source code of string builder class.
And just to remind you, what we are going to review now is absolutely similar in string.
The buffer clause was only one exception.
Synchronized methods.
This is the full constructor calls, constructor of super class.
That is abstract string the builder specifying the original capacity of the characters array.
By default it is 610.
Do not find any Secrett logic behind this number.
It is just 16.
There is also a string builder within parameter to specify any custom default capacity.
One you should use this version of constructor.
I believe that you already know how arrays work in Java.
You can text extend array that was created already.
So to extent existing array you need.
To create a bigger array and copy old values in that this is time consuming operation.
That's why in case you know that you're going to process a big amount of data, you can set initial
capacity in order to avoid concentration of new array objects, to have enough space for all characters.
Also, we can create string builder and initialize it with the content of the string object or any char
sequence.
Short sequence is an interface that is implemented by many different classes in Java, for example,
string swing builder, string buffer, abstract string builder and others.
All of them implements char sequence interface.
This interface declares contract for such mascot's as Lancs to return total number of characters in
char sequence char at Richmondshire as a specified index is empty default masset the checks with the
Lancs equal to zero returns.
True, if character sequence is empty and false, otherwise Subsequence Masset will return char sequence
from indexing collusively to indexed exclusively.
And you know what the string method does.
Also, we are a few more default methods and one Zem Chass returns in stream.
Just to remind you that Char easily may be converted to end Y because integer variable specifies sequence
number of the character and the Unicode table code points returns in stream where each end will represent
characters sequence number in the Unicode table.
I will be honest with you, I have never used this method, but at least it is good just to know that
such as it exists in learning purposes and static methods that may be used to combat two character sequences.
Let's get back to the implementation of a sequence that we are running today.
I open string builder class.
As you can see in outline here, there are a lot of versions of append method.
This method is overloaded to be able to accept parameter of different types.
Also, we can delete from char to char, from string builder onto the teacher at the specified index.
And again, it is hard for me to come up with an example where you would use this massas on a daily
basis, replace Masset from index to index and be replaced with the string.
Another method that is overloaded in this class is insert.
It is used to insert the specified value at the specified position.
It is overloaded.
The Different Parameters Index of Masset returns the index within this string of the first occurrence
of the specified substring last index of returns.
The index within this string of the last occurrence of the specified substring.
The last two methods also have overloaded version that additionally it takes one more parameter of time.
This is the index from which to start the search reverse.
Massata causes this character sequence to be replaced by the reverse of the sequence.
That's all methods of string builder class.
What I would recommend you open the source code of string builder string, buffer and abstract string
to build the class leukocoria through the code and try to play with matches.
We have discussed the most important ones and the masses that have chance to be used in production.
But in case you would find any other method that you are interested in and you would like to talk more
about it, do not hesitate to ask questions below this video.
I will be happy to answer all of your questions, but I truly believe that we have learned just enough
things in order to be able to start using string builder and string four classes.
Now, let's recap what we have learned in this lesson.
After this lesson, we know what string builder and string buffer classes are.
I believe that you understood the purpose of these classes and use cases when to use them.
Also review the char sequence interface today.
That's it for this lesson.
Thanks a lot for your attention.
Have a great day and see you in the next lesson.