Java Essentials - String buffer and string builder in java
8K views
Oct 26, 2024
Java Essentials - String buffer and string builder in java
View Video Transcript
0:00
String Buffer and String Builder in Java
0:04
So here we are going to discuss string buffer and string builder and their respective comparisons
0:10
We know that in case of string objects, we are having one problem. There is the string objects are immutable
0:16
That is after defining the string object, it is not possible for us to update the string object with the new values
0:24
So in that case, we are having some problem. So those problems will get resolved in our search
0:29
in our string buffer and string builder in our Java so the string buffer and string
0:35
builder classes are used when there is a necessity to make a lot of modifications
0:40
out to the strings of characters whenever those applications where this frequent and rapid update in the string content is going to take place there you
0:50
can go for this string buffer and string builder class objects so unlike strings
0:55
objects of type string buffer and string builder can be modified over and over
1:00
again without leaving behind a lot of new unused objects so that's why the
1:07
memory utilization will be fruitful and we can go on doing the updates on this string buffer and string builder class objects as when necessary as many number of times we want
1:20
It is recommended to use the string builder whenever possible because it is faster than
1:25
string buffer. So it is recommended that instead of using the string buffer class objects, we can easily
1:31
use the string builder class objects because their operations will be a little bit faster
1:36
If you consider the time complexity, then you should go for the store. string builder class objects handling
1:42
But however, if the thread safety is necessary, the best option is the string buffer objects
1:49
That means whenever we are working on a multi-threaded environment, that means in that
1:54
very particular case, when the multiple threads, that means the lightweight processes
1:58
are executing in parallel in those cases, for the threat safety, this string, that is
2:05
however it is that the string buffer objects are the best choices
2:10
compared to the string builder. So, let us discuss more on this thread sette
2:14
So thread set code is only manipulates shared data structure in a manner that ensures that
2:21
always the threads behave properly and fulfill their operations on the shared data structure without having unintended interactions And that is the main definition of this thread septive
2:35
So some frequently use functions for string buffer and string builder classes
2:41
So here we have mentioned some of them which we use on a very regular basis
2:45
Capacity. It returns the current capacity of the string buffer and the string builder objects
2:50
So now we are having this append. So from this very function name, it is quite
2:54
obvious that what is going to happen that is insert new strings at the end of the previous
3:00
string next one is the insert this method inserts a string at the position mentioned by the
3:07
offset given next one is the reverse and reverse the sequence of characters in the string
3:13
so these are the very common usable methods both in case of string builder and string buffer
3:20
objects and you'll be using them in our code so let us go for one practical demonstration
3:24
on this concept for your better understanding in this demonstration we're going to
3:31
discuss string buffer and string builder class objects at first we're dealing
3:35
with the string buffer so here you can find that one string buffer object class object has been defined that is s buffer which has been instantiated with this constructor here and the default capacity of this S buffer will be 16 here so let us go for the execution you can find that when you are printing this is
3:52
buffer dot capacity you are getting here 15 this is buffer is buffered is
3:58
now instantiated with the string buffer constructor and here we're setting the capacity directly as 50 here as a result of that when you were setting that
4:07
one so it is playing it is having the capacity So now again we are defining this is buffer with this string buffer construct 330 and here we are passing one string as input argument then what will happen the 16 plus the length of the string will be the total buffer capacity
4:26
What is the length of the string if you go on counting? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 so 15 characters are there plus 16 so you are expecting the capacity would become 3 3 3rd 6
4:39
31 here and here we're printing the S buffer directly so S buffer has got printed
4:45
that is my string buffer next we're dealing with the string builder objects
4:51
so string builder S builder is equal to new string builder my string builder so
4:56
this and string which will initialize this S builder
#Java (Programming Language)