java-development-for-beginners-learnit / 31 - JDK API /001 StringBuilder & StringBuffer_en.srt
Tan115's picture
Add files using upload-large-folder tool
07c2d26 verified
Raw
History Blame Contribute Delete
19.2 kB
1
00:00:05,000 --> 00:00:06,000
Hello, Jim.
2
00:00:06,000 --> 00:00:10,000
In this lesson, we are going to learn how to work with swing builder and string buffer classes.
3
00:00:11,000 --> 00:00:14,000
Also, I'm going to explain why we might need them.
4
00:00:14,000 --> 00:00:21,000
We're going to start from understanding what these classes are and why do we need them and want to understand
5
00:00:21,000 --> 00:00:23,000
the problems that these classes are supposed to solve.
6
00:00:24,000 --> 00:00:29,000
We're going to have practical example where I will explain in detail why you should never forget about
7
00:00:29,000 --> 00:00:33,000
string builder and string the buffer in this lesson.
8
00:00:33,000 --> 00:00:38,000
I'm going to explain the difference between these two classes and what is even more important.
9
00:00:38,000 --> 00:00:42,000
I will teach you when to use these classes and how to use them.
10
00:00:42,000 --> 00:00:50,000
I will show you example of string builder and string the bar for usage and also will char sequence interface.
11
00:00:50,000 --> 00:00:57,000
Let's start let's try to understand first what a string builder class in the documentation.
12
00:00:57,000 --> 00:01:02,000
This class is described as mutable sequence of characters by this moment.
13
00:01:02,000 --> 00:01:07,000
Now, of course, you already know that objects of string class are immutable.
14
00:01:07,000 --> 00:01:13,000
In short, there's a class designed in the way that you can't change the state of a regional object
15
00:01:13,000 --> 00:01:15,000
after it was instantiated.
16
00:01:16,000 --> 00:01:21,000
All assets don't modify state of the original string or objects that we created.
17
00:01:22,000 --> 00:01:28,000
The class itself is final and you can't override it to change the logic of methods.
18
00:01:28,000 --> 00:01:35,000
That means that each time you modify string in your program, you actually create a new object of the
19
00:01:35,000 --> 00:01:35,000
string.
20
00:01:36,000 --> 00:01:42,000
Yes, you might lose the reference to the string object in case new string object is assigned to the
21
00:01:42,000 --> 00:01:43,000
regional variable.
22
00:01:43,000 --> 00:01:49,000
And yes, in the case there is no strong references to the object, garbage collector will clean it
23
00:01:49,000 --> 00:01:49,000
up.
24
00:01:50,000 --> 00:01:53,000
I'm going to show this case in the example later today.
25
00:01:53,000 --> 00:01:57,000
But the only question that I want to ask you sounds like this.
26
00:01:58,000 --> 00:02:02,000
Do we need to rely on garbage collector and make it work?
27
00:02:02,000 --> 00:02:07,000
The answer is no one garbage collector starts its work.
28
00:02:07,000 --> 00:02:12,000
It is also some computation time that potentially might be used by US operations.
29
00:02:12,000 --> 00:02:16,000
Instead of cleaning up objects without strong references.
30
00:02:16,000 --> 00:02:22,000
And by the way, it is important for you to know and remember the difference between different types
31
00:02:22,000 --> 00:02:23,000
of references.
32
00:02:23,000 --> 00:02:29,000
In case you want to learn more about this topic, make sure you watched my lesson about references where
33
00:02:29,000 --> 00:02:36,000
I explained different types of references, but still how to solve a problem of constant creation of
34
00:02:36,000 --> 00:02:37,000
string object.
35
00:02:37,000 --> 00:02:44,000
If I'm modifying string so the situation might be completely different is a process a lot of strings
36
00:02:44,000 --> 00:02:50,000
and you need all of them in one string or you prepare string before writing it to the file.
37
00:02:51,000 --> 00:02:52,000
It doesn't matter.
38
00:02:52,000 --> 00:02:59,000
But in some hypothetical case, instead of creation, millions of string objects and heap memory, it
39
00:02:59,000 --> 00:03:04,000
is recommended to create one object of string, build it up and use it.
40
00:03:04,000 --> 00:03:09,000
Does it make sense to understand the motivation of using an object of this type?
41
00:03:10,000 --> 00:03:10,000
Great.
42
00:03:11,000 --> 00:03:14,000
Let's now understand what a string buffer class is.
43
00:03:14,000 --> 00:03:21,000
It would be easy to explain what is this class about because it is almost the same as string builder
44
00:03:21,000 --> 00:03:21,000
class.
45
00:03:22,000 --> 00:03:24,000
They have similar methods, similar constructors.
46
00:03:25,000 --> 00:03:27,000
Literally almost everything is similar.
47
00:03:27,000 --> 00:03:35,000
The only difference is that string, the buffer is a safe class and objects of this type might be used
48
00:03:35,000 --> 00:03:39,000
in an execution without impact on data consistency.
49
00:03:39,000 --> 00:03:43,000
What a lot of training is discussed in the separate section of the course.
50
00:03:44,000 --> 00:03:51,000
But still, I would like to remind to all students about multithreaded one more time in in case of Perou
51
00:03:51,000 --> 00:03:53,000
execution of multiple threads.
52
00:03:54,000 --> 00:04:00,000
When different threads update the state of the string buffer simultaneously or read its data, only
53
00:04:00,000 --> 00:04:07,000
one thread is allowed to execute these operations with potential mistakes that may happen because of
54
00:04:08,000 --> 00:04:09,000
an axis.
55
00:04:09,000 --> 00:04:15,000
That's why everything what I'm going to share with you about String Builder is absolutely applicable
56
00:04:15,000 --> 00:04:17,000
to the string buffer in Montessori environment.
57
00:04:18,000 --> 00:04:24,000
The rule of thumb is to use three builder whenever is possible because it has better performance.
58
00:04:25,000 --> 00:04:31,000
This is just consequences of not having synchronization from multiple threads inside string builder,
59
00:04:31,000 --> 00:04:38,000
whereas all mascot's in string buffer are synchronized and this takes additional overhead during the
60
00:04:38,000 --> 00:04:39,000
execution.
61
00:04:39,000 --> 00:04:41,000
You should be all right with String Builder.
62
00:04:41,000 --> 00:04:47,000
In most cases, I believe that you already understood on the high level why we need these classes.
63
00:04:47,000 --> 00:04:53,000
It is time to dive deeper in details and understand how to work with objects of this type.
64
00:04:54,000 --> 00:04:57,000
And let's start from explaining the hypothetical problem.
65
00:04:58,000 --> 00:04:59,000
I have file with stacks.
66
00:05:00,000 --> 00:05:03,000
This is lyrics of the song that I listened while.
67
00:05:03,000 --> 00:05:09,000
Thinking about the structure of the lesson, the song is called Never Coming Home By Staying Pretty
68
00:05:10,000 --> 00:05:11,000
Cool one, by the way.
69
00:05:11,000 --> 00:05:18,000
And according to business logic, I need to be able to read text from files and after that work with
70
00:05:18,000 --> 00:05:20,000
the content of the file as a whole.
71
00:05:20,000 --> 00:05:24,000
For example, text analysis or text statistics is needed.
72
00:05:25,000 --> 00:05:29,000
Probably you want to implement programs that would analyze thousands of songs in order.
73
00:05:29,000 --> 00:05:36,000
You could find the best rhymes any way you can be in any of other situations that would make it to collect
74
00:05:36,000 --> 00:05:39,000
all streams in one string object.
75
00:05:39,000 --> 00:05:47,000
I open my example now that is called string build a demo I have mastered that reads all lines from file
76
00:05:47,000 --> 00:05:52,000
into one string object and after that I bring the strings to console.
77
00:05:52,000 --> 00:05:54,000
Let's review this method.
78
00:05:54,000 --> 00:06:00,000
I create the reference of the same time as it is pointing out to the object that is stored in St. Paul.
79
00:06:01,000 --> 00:06:07,000
If you don't remember what a string will please refer to as a lesson about string objects and string
80
00:06:07,000 --> 00:06:09,000
CPU in my Java course.
81
00:06:09,000 --> 00:06:14,000
In the next line I read all lines from file into the list of strings.
82
00:06:14,000 --> 00:06:20,000
In case you're interested how I did this, please refer to the section about input output streams in
83
00:06:20,000 --> 00:06:21,000
my Java course.
84
00:06:21,000 --> 00:06:29,000
After that I iterate and over each line and I concatenate each line with my result string and adding
85
00:06:29,000 --> 00:06:31,000
new one character at the end of each line.
86
00:06:32,000 --> 00:06:34,000
What is actually happening here?
87
00:06:34,000 --> 00:06:41,000
I create a new string object after each concatenation and this string reference pointing out to the
88
00:06:41,000 --> 00:06:42,000
new object in memory.
89
00:06:42,000 --> 00:06:48,000
Each time after concatenation you are going to have the object that contains the first line.
90
00:06:48,000 --> 00:06:52,000
Only the second object would be the first line.
91
00:06:52,000 --> 00:06:59,000
And Newline character is a shared object, would be the first line newline character and the second
92
00:06:59,000 --> 00:07:00,000
line and so on.
93
00:07:01,000 --> 00:07:05,000
Why do you need so many duplication and so many string objects?
94
00:07:05,000 --> 00:07:09,000
And that is not a big harm in process in one song.
95
00:07:09,000 --> 00:07:15,000
But believe me, you are going to work with files much bigger than we have in our example today.
96
00:07:15,000 --> 00:07:17,000
And this is going to become a problem.
97
00:07:18,000 --> 00:07:19,000
What would be the solution here?
98
00:07:19,000 --> 00:07:22,000
The solution here is to use string build object.
99
00:07:23,000 --> 00:07:29,000
Let's look at another method that also read all lines from the file, but do it with help of string
100
00:07:29,000 --> 00:07:30,000
builder object.
101
00:07:30,000 --> 00:07:34,000
I create object of string, build a type with the default constructor.
102
00:07:35,000 --> 00:07:37,000
After that I am doing the same thing.
103
00:07:37,000 --> 00:07:39,000
I'm bringing the lines into the list.
104
00:07:39,000 --> 00:07:41,000
Object in foreach loop.
105
00:07:41,000 --> 00:07:45,000
I call append massata patterns new line and newline character.
106
00:07:46,000 --> 00:07:51,000
I need to return a string object from the method so I call to string methods.
107
00:07:51,000 --> 00:07:54,000
It creates string object based on my string builder.
108
00:07:55,000 --> 00:07:55,000
That's it.
109
00:07:56,000 --> 00:08:02,000
To prove you that concatenation of strings with string builder is more efficient, I evaluate the time
110
00:08:02,000 --> 00:08:04,000
for both of these separations.
111
00:08:04,000 --> 00:08:11,000
I just take amount of milliseconds before the first method invocation and after that I calculate Delta
112
00:08:11,000 --> 00:08:19,000
in milliseconds by the doctors the time that I already have from current time after method execution.
113
00:08:19,000 --> 00:08:24,000
Following this approach, I can estimate how long does it take to execute code?
114
00:08:24,000 --> 00:08:26,000
That is between two points.
115
00:08:26,000 --> 00:08:30,000
And I did similar measurement in the second example for String Builder.
116
00:08:31,000 --> 00:08:37,000
After that I bring two values to console in order we could have a chance to compare these two values.
117
00:08:37,000 --> 00:08:45,000
Let's run the program so you can see the difference in performance with one song of string is approximately
118
00:08:45,000 --> 00:08:47,000
ten times more restrained.
119
00:08:47,000 --> 00:08:49,000
Object concatenation.
120
00:08:49,000 --> 00:08:52,000
Imagine what the difference would be was a gigabyte files.
121
00:08:53,000 --> 00:08:54,000
It is crazy.
122
00:08:54,000 --> 00:08:59,000
That's why it is important to use string builder during the processing of big number of strings.
123
00:09:00,000 --> 00:09:06,000
Basically, we have just removed probably the most popular example of string builder and string bar
124
00:09:06,000 --> 00:09:07,000
for usage.
125
00:09:07,000 --> 00:09:12,000
But let me also share with you a little bit more details about class structure and other methods.
126
00:09:13,000 --> 00:09:15,000
I open the source code of string builder class.
127
00:09:16,000 --> 00:09:21,000
And just to remind you, what we are going to review now is absolutely similar in string.
128
00:09:21,000 --> 00:09:23,000
The buffer clause was only one exception.
129
00:09:23,000 --> 00:09:25,000
Synchronized methods.
130
00:09:25,000 --> 00:09:28,000
This is the full constructor calls, constructor of super class.
131
00:09:29,000 --> 00:09:35,000
That is abstract string the builder specifying the original capacity of the characters array.
132
00:09:35,000 --> 00:09:37,000
By default it is 610.
133
00:09:38,000 --> 00:09:41,000
Do not find any Secrett logic behind this number.
134
00:09:41,000 --> 00:09:42,000
It is just 16.
135
00:09:43,000 --> 00:09:48,000
There is also a string builder within parameter to specify any custom default capacity.
136
00:09:49,000 --> 00:09:51,000
One you should use this version of constructor.
137
00:09:52,000 --> 00:09:55,000
I believe that you already know how arrays work in Java.
138
00:09:55,000 --> 00:09:59,000
You can text extend array that was created already.
139
00:10:00,000 --> 00:10:02,000
So to extent existing array you need.
140
00:10:03,000 --> 00:10:09,000
To create a bigger array and copy old values in that this is time consuming operation.
141
00:10:10,000 --> 00:10:16,000
That's why in case you know that you're going to process a big amount of data, you can set initial
142
00:10:16,000 --> 00:10:23,000
capacity in order to avoid concentration of new array objects, to have enough space for all characters.
143
00:10:23,000 --> 00:10:30,000
Also, we can create string builder and initialize it with the content of the string object or any char
144
00:10:30,000 --> 00:10:30,000
sequence.
145
00:10:31,000 --> 00:10:37,000
Short sequence is an interface that is implemented by many different classes in Java, for example,
146
00:10:37,000 --> 00:10:42,000
string swing builder, string buffer, abstract string builder and others.
147
00:10:42,000 --> 00:10:45,000
All of them implements char sequence interface.
148
00:10:46,000 --> 00:10:53,000
This interface declares contract for such mascot's as Lancs to return total number of characters in
149
00:10:53,000 --> 00:11:01,000
char sequence char at Richmondshire as a specified index is empty default masset the checks with the
150
00:11:01,000 --> 00:11:03,000
Lancs equal to zero returns.
151
00:11:03,000 --> 00:11:10,000
True, if character sequence is empty and false, otherwise Subsequence Masset will return char sequence
152
00:11:10,000 --> 00:11:14,000
from indexing collusively to indexed exclusively.
153
00:11:14,000 --> 00:11:16,000
And you know what the string method does.
154
00:11:17,000 --> 00:11:22,000
Also, we are a few more default methods and one Zem Chass returns in stream.
155
00:11:23,000 --> 00:11:31,000
Just to remind you that Char easily may be converted to end Y because integer variable specifies sequence
156
00:11:31,000 --> 00:11:40,000
number of the character and the Unicode table code points returns in stream where each end will represent
157
00:11:40,000 --> 00:11:42,000
characters sequence number in the Unicode table.
158
00:11:43,000 --> 00:11:49,000
I will be honest with you, I have never used this method, but at least it is good just to know that
159
00:11:49,000 --> 00:11:56,000
such as it exists in learning purposes and static methods that may be used to combat two character sequences.
160
00:11:57,000 --> 00:12:02,000
Let's get back to the implementation of a sequence that we are running today.
161
00:12:02,000 --> 00:12:04,000
I open string builder class.
162
00:12:04,000 --> 00:12:10,000
As you can see in outline here, there are a lot of versions of append method.
163
00:12:11,000 --> 00:12:16,000
This method is overloaded to be able to accept parameter of different types.
164
00:12:16,000 --> 00:12:23,000
Also, we can delete from char to char, from string builder onto the teacher at the specified index.
165
00:12:23,000 --> 00:12:28,000
And again, it is hard for me to come up with an example where you would use this massas on a daily
166
00:12:28,000 --> 00:12:35,000
basis, replace Masset from index to index and be replaced with the string.
167
00:12:36,000 --> 00:12:39,000
Another method that is overloaded in this class is insert.
168
00:12:40,000 --> 00:12:44,000
It is used to insert the specified value at the specified position.
169
00:12:44,000 --> 00:12:45,000
It is overloaded.
170
00:12:45,000 --> 00:12:53,000
The Different Parameters Index of Masset returns the index within this string of the first occurrence
171
00:12:53,000 --> 00:12:58,000
of the specified substring last index of returns.
172
00:12:58,000 --> 00:13:03,000
The index within this string of the last occurrence of the specified substring.
173
00:13:03,000 --> 00:13:10,000
The last two methods also have overloaded version that additionally it takes one more parameter of time.
174
00:13:11,000 --> 00:13:15,000
This is the index from which to start the search reverse.
175
00:13:15,000 --> 00:13:21,000
Massata causes this character sequence to be replaced by the reverse of the sequence.
176
00:13:21,000 --> 00:13:24,000
That's all methods of string builder class.
177
00:13:24,000 --> 00:13:30,000
What I would recommend you open the source code of string builder string, buffer and abstract string
178
00:13:30,000 --> 00:13:36,000
to build the class leukocoria through the code and try to play with matches.
179
00:13:36,000 --> 00:13:41,000
We have discussed the most important ones and the masses that have chance to be used in production.
180
00:13:42,000 --> 00:13:48,000
But in case you would find any other method that you are interested in and you would like to talk more
181
00:13:48,000 --> 00:13:51,000
about it, do not hesitate to ask questions below this video.
182
00:13:51,000 --> 00:13:57,000
I will be happy to answer all of your questions, but I truly believe that we have learned just enough
183
00:13:57,000 --> 00:14:02,000
things in order to be able to start using string builder and string four classes.
184
00:14:03,000 --> 00:14:06,000
Now, let's recap what we have learned in this lesson.
185
00:14:06,000 --> 00:14:10,000
After this lesson, we know what string builder and string buffer classes are.
186
00:14:11,000 --> 00:14:17,000
I believe that you understood the purpose of these classes and use cases when to use them.
187
00:14:17,000 --> 00:14:20,000
Also review the char sequence interface today.
188
00:14:20,000 --> 00:14:22,000
That's it for this lesson.
189
00:14:22,000 --> 00:14:24,000
Thanks a lot for your attention.
190
00:14:24,000 --> 00:14:27,000
Have a great day and see you in the next lesson.