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