Not it's look at string concatenation string concatenation involves concatenating strings together that 2
is with joined strings together. 3
We all know that a plus operator can be used for that. 4
And here is an example. 5
If you want to additionally concatenate a pneumatic 125. 6
We could have done this Alternatively you can also do this. 7
That is we simply add the indigent 125 undergrad we are took on what administering. 8
So the plus operator gives precedence to a string operation. 9
And so any non string or print will be automatically converted to a string. 10
So it's a little bit like the mixed up operations rule where smaller type operand is automatically promoted 11
with a larger type. 12
And this because you can think of string as the larger type not want to also concatenate a double value 13
. 14
We can do this not that here the last two numeric values are not honored but they are simply concatenated 15
. 16
This is because there is no parenthesis and there is only a single operator in the expression. 17
So if you recall in this because the expression is evaluated from left to right. 18
So faster two strings. 19
How long will it be concatenated and then press will be applied on the string Hello world. 20
The numeric 125. 21
The resulting string will then be finally concatenated with a numeric only 5.5. 22
Hello or if he had this exhibition we're numerics or more with a friend than the two numeric values 23
will be added first as evaluation happens from left to right. 24
The resulting value will then be concatenated with the following strings. 25
String concatenation can also be performed using string builder and string buffer classes from the Djala 26
Dr. Lang package while string class is immutable. 27
These two classes allow us to mutate strings that is these two classes allow their contents to be changed 28
let's not look at string builder class it was introduced in Jollof I on here is an example of its usage 29
Foster string will that object is created and then it's a benefit that is involved to append new content 30
. 31
So fast the string hello is honored and then the string world is up undecorated next a single statement 32
is used to append two more strings not that each append gone returns a reference to the current object 33
which is allowing in multiple append calls in the same state and finally posting method is invoked which 34
returns a string that is initialized with a character sequence in the string builder so string as has 35
the entire text Hello world. 36
Good morning string will that also has many other matters. 37
And here are a few of them. 38
You can use that matter to know the length of the string constructor. 39
So. 40
Need math that can be used to delete characters between two indices Similarly there are methods for 41
inserting content or to specify an index. 42
There is also a method to reverse a string. 43
Finally there is also a method to replace certain substrings with the specified strings. 44
Most of the time you will be using a pen and that boosting matters at the end of the lesson will be 45
a short term or maybe we'll try out some of these methods also as you can see unlike string class string 46
will there is more to have happen and does it allows us to modify its content. 47
Finally string builder is not synchronized synchronization is a concurrency concept and you look at 48
it later when studying concurrency. 49
But that just gives you an idea if the same string builder object is shared across two threads then 50
one thread can modify the contents of the object when the other is already modifying the seeing content 51
. 52
So there is a simultaneous modification of the content and this can be good data corruption issues and 53
this can happen because string builder is not synchronized but if synchronization is critical it can 54
be taken care of externally and that we will see Randstad in concurrency know before signing a bill 55
that was introduced in July 5 string buffer was being used and it works just like string builder that 56
is it is like String class but can be modified. 57
However with the introduction of string builder class it is recommended to use string builder as it 58
is faster than string Barbar. 59
The main reason is because string before is synchronized and synchronization slows down the program 60
. 61
This is because synchronization has to ensure that at any particular time only one thread is modifying 62
string first content. 63
Well synchronization is critical in the context of multi-threading. 64
It was found to be an overhead in the context of using string buffer. 65
This is because string buffer was mostly being used by a single thread and programmers were not usually 66
sharing it across tracks. 67
So the language designers decided to introduce string builder and also allowed developers to use it 68
as a drop in replacement for string buffer string buffer is being used by only a single thread to ensure 69
this string builder was designed to have an API that is compatible with string buffer. 70
So just look on the Java docs for both these classes and you'll be able to see the similarities non-Disney 71
a short term or string concatenation 72
in the interest of time I'm going to show this to mind my eclipse the I and we're going to try out all 73
three weeks off concatenating strings that is using press operator then the two class a string builder 74
string buffer. 75
OK so let's begin with this plus all pricker So let's just create a string and let's just concatenate 76
two strings. 77
Hello world. 78
It's just print this 79
I mean just from this and it's printing out here next let's create string buffer so let's create a string 80
but for instance 81
and we can pass this same string reference S here so that where we are initialising string buffer with 82
this. 83
So there is a constructor for that next letter. 84
A pen couple of our new strings to this to the string Hello world. 85
So let's just call a band met they're and that's been the key word good and since then returns a reference 86
to the cut on String object to the current string buffer object we can use one more up and method here 87
so we can change it this way as we have seen earlier in the slightest and that's a morning and let's 88
put a smiley here. 89
And that's it. 90
Now let's just grand 91
the strength so there's just as be taught to string that arrogance does a string that frequences string 92
going on. 93
That's also print the land I need to say the it so now let's print this. 94
So as you can see as B is now. 95
Hello world. 96
Good morning. 97
On the left is 29. 98
OK. 99
So that's the length of the string that we construct so far. 100
Now we also have a couple of other matters like we discussed about other methods like delete and insert 101
. 102
Let's just try or delete an insert now. 103
So let's say we want to delete contactors starting with the next one and ending with the next five. 104
So here one is inclusive but and five is exclusive so. 105
So we wanted to lead the characters from starting from one to the index for OK not five. 106
So let's again reprint this the same length and distrain And as you can see the characters E L L O. 107
Which is it starts with the index one and an index for you know are are just removed are just deleted 108
. 109
So since four characters have been deleted the plant is now 25. 110
Now let's insert a couple of garters. 111
So let's just set it s b dark. 112
Insert one so you want to insert the next one and say you want to next the string you write so that 113
rate will be previous her alone I was going to be here. 114
So that's just then the message is once again be the string Underland. 115
Now you can see that the characters yway got added here. 116
So we have hello. 117
Hey hey. 118
Well good morning Underland is not only seven so that's how you can you know you can mutate the string 119
with just a string. 120
It is immutable. 121
But here when you use string before we can mutate string and we said that with string after five point 122
off a dollar fine you know we can use a shingle as it is much more efficient. 123
So we can have it like this. 124
So all we need to do is just replace string buffer with string builder and everything works the same 125
. 126
So the language designers the you know they make sure that you know that the API is exactly identical 127
so they have to support all the networks on you know on them at their signatures so that we can use 128
it as a drop in replacement. 129
You know instead of staring before and using string builder is much faster if synchronization is not 130
needed. 131
OK. 132
So that's important. 133
So we looked at both plus plus singular osting buffer before string builder or string buffer. 134
Next we're going to look at an item from Effective Java and it is related to a string concatenation 135
and it specifically talks about String about the plus operator on some performance implications that 136
are honored. 137
So that would be the next lesson. 138
OK right then. 139
Thank you and happy coding.