1

00:00:01,200  -->  00:00:05,250
Next is a recommendation from Effective Java I am 51.

2

00:00:05,250  -->  00:00:09,070
It says beware the performance of string concatenation.

3

00:00:09,310  -->  00:00:11,900
Let's see what exactly it has to say.

4

00:00:12,810  -->  00:00:18,810
It says a bless operator is a convenient way to combine few strings and it shouldn't be used to combine

5

00:00:18,810  -->  00:00:23,200
more than few strengths unless performance is irrelevant.

6

00:00:24,270  -->  00:00:30,990
This is because with each concatenation these steps are being performed condense of both strings are

7

00:00:30,990  -->  00:00:37,620
first copied then a new String object is created and is appended with both the strings.

8

00:00:37,620  -->  00:00:43,650
Finally a new string is generated by invoking the string method on the string builder object.

9

00:00:43,650  -->  00:00:47,310
So these steps are being performed with each concatenation.

10

00:00:47,310  -->  00:00:49,440
Let's look at an example.

11

00:00:50,340  -->  00:00:55,690
Let's assume there is an array with three strings A B and C that are to be concatenated in a for loop

12

00:00:55,730  -->  00:00:56,090
.

13

00:00:56,730  -->  00:01:02,940
That is each iteration appends a string from the RV to understand the performance implication of using

14

00:01:02,940  -->  00:01:04,290
Pless operator.

15

00:01:04,290  -->  00:01:09,610
Let's consider the following statements which correspond to each iteration and the follow up.

16

00:01:10,230  -->  00:01:15,540
Let's assume s to be a string variable declared outskirt a for loop and is initialized with an empty

17

00:01:15,540  -->  00:01:17,220
string.

18

00:01:17,280  -->  00:01:22,290
The first statement corresponds to a statement execution during the first iteration and it is a concatenation

19

00:01:22,290  -->  00:01:25,130
between an empty string and a string.

20

00:01:25,740  -->  00:01:32,090
What does a copy of the string under string ARMH first and then they are concatenated in the second

21

00:01:32,100  -->  00:01:37,910
statement string as is up and there will be a copy of the current value of s.

22

00:01:37,920  -->  00:01:45,390
Registering a is made along with a copy of B and then the concatenation is performed similarly in the

23

00:01:45,390  -->  00:01:46,470
final statement.

24

00:01:46,560  -->  00:01:53,040
A copy of the current value of S is made with a string AB along with the copy of C and then the concatenation

25

00:01:53,040  -->  00:01:54,690
is performed.

26

00:01:54,690  -->  00:02:01,710
Also note that a string builder object is constructed for each of these concatenations.

27

00:02:01,710  -->  00:02:08,430
So as you can see it is quite expensive to use plus operator time complexity is quadratic as we have

28

00:02:08,430  -->  00:02:15,510
to create a copy of previously created string in each iteration that is and iteration requires a copy

29

00:02:15,510  -->  00:02:16,100
or string.

30

00:02:16,100  -->  00:02:19,040
Million and minus Vonte iteration.

31

00:02:19,050  -->  00:02:24,420
Similarly it is best consuming to add several strings that are getting created along with dust string

32

00:02:24,420  -->  00:02:27,450
builder objects.

33

00:02:27,450  -->  00:02:33,100
So item 51 recommends to use trng builder as it has a linear complexity.

34

00:02:33,240  -->  00:02:38,910
There is only one string builder object can be created out of the follow on in each iteration.

35

00:02:38,950  -->  00:02:45,330
A string can be appended to this object according to one benchmark string builder was found to be 300

36

00:02:45,330  -->  00:02:48,390
times faster than using plus operator.

37

00:02:48,390  -->  00:02:53,550
Similarly string builder was also found to be twice as fast as a string buffer.

38

00:02:53,610  -->  00:02:58,950
In this particular benchmark a string is constructed by looping for Iran sixteen thousand five hundred

39

00:02:58,950  -->  00:03:00,020
times.

40

00:03:00,090  -->  00:03:04,890
If you're interested in this benchmark just look at the link provided in the resource section.

41

00:03:05,460  -->  00:03:06,530
And that's about it.

42

00:03:06,540  -->  00:03:07,800
So you string builder.

43

00:03:07,800  -->  00:03:09,550
If performance is important.

44

00:03:09,690  -->  00:03:13,270
Use Pless operator only to combine fuse strings.

45

00:03:13,500  -->  00:03:13,980
Thank you
