1 00:00:08,310 --> 00:00:12,260 In this lecture, we'll find the answer to the question 43. 2 00:00:12,570 --> 00:00:16,200 What is the difference between string and string builder? 3 00:00:16,770 --> 00:00:21,000 String is a type used for representing textual data. 4 00:00:21,420 --> 00:00:27,510 String Builder is a utility class created for optimal concatenation of strings. 5 00:00:28,080 --> 00:00:29,520 We all know strings. 6 00:00:29,730 --> 00:00:32,760 We use them all the time to represent some text. 7 00:00:33,180 --> 00:00:39,720 What some people don't know is that all strings are immutable, which means once created, they can't 8 00:00:39,720 --> 00:00:40,680 be quantified. 9 00:00:41,100 --> 00:00:44,910 If you haven't watched the previous lecture, you may be a bit surprised. 10 00:00:45,120 --> 00:00:48,360 You probably mutated strings plenty of times by now. 11 00:00:48,720 --> 00:00:52,110 For example, let's try to modify this string. 12 00:00:58,000 --> 00:00:59,530 So what's the big deal? 13 00:00:59,860 --> 00:01:01,330 We just modified a string. 14 00:01:01,840 --> 00:01:09,070 First Lieutenant, the value from ABC to the IMF, which obviously is a modification, and then we are 15 00:01:09,090 --> 00:01:14,020 the retreat which resulted in the final value of the TFG. 16 00:01:14,440 --> 00:01:17,500 So what's the fuss about the immutability? 17 00:01:18,040 --> 00:01:22,570 Well, the thing is, we actually didn't modify the ABC string. 18 00:01:22,900 --> 00:01:30,910 We created our brand new DFA string, and we simply pointed the reference stored in some string variable 19 00:01:31,090 --> 00:01:32,200 to this new string. 20 00:01:32,830 --> 00:01:35,380 A similar thing happened in the next line. 21 00:01:35,710 --> 00:01:43,750 We created a new string by concatenating the F with G and then pointed the reference to this new string 22 00:01:44,380 --> 00:01:45,820 at some point in time. 23 00:01:46,060 --> 00:01:52,960 The garbage collector will see those old strings as objects to whom no reference points and will remove 24 00:01:52,960 --> 00:01:54,190 them from memory. 25 00:01:54,670 --> 00:02:02,200 So we now know that when we modify a value of string, the following things need to happen a new object 26 00:02:02,200 --> 00:02:06,100 needs to be created, which involves allocating memory for it. 27 00:02:06,400 --> 00:02:12,790 The variable that was pointing to the original string must be pointed to the new string at some point. 28 00:02:12,970 --> 00:02:18,850 The garbage collector must clean out the old string that's relatively out of work. 29 00:02:19,240 --> 00:02:26,530 In many cases, it's OK, and we don't notice any performance impact when we add Mr. to the John Smith 30 00:02:26,540 --> 00:02:27,100 string. 31 00:02:27,550 --> 00:02:31,330 But I need to build strings gradually from parts. 32 00:02:31,450 --> 00:02:32,500 It's pretty common. 33 00:02:32,980 --> 00:02:39,820 For example, imagine your application is done holding some data from the web and it does it in chunks 34 00:02:39,970 --> 00:02:41,830 as the data is pretty large. 35 00:02:42,280 --> 00:02:47,700 You need to create some kind of extract from this data for each task, right? 36 00:02:47,710 --> 00:02:49,510 And it can be thousands of them. 37 00:02:49,780 --> 00:02:56,800 You need to build some pretty complex string and then append district the string representing the final 38 00:02:56,800 --> 00:02:57,430 result. 39 00:02:57,820 --> 00:03:03,700 It can involve millions of concatenation operations as the process continues. 40 00:03:03,730 --> 00:03:05,710 The final result is growing. 41 00:03:06,160 --> 00:03:08,710 At some point it can be a huge string. 42 00:03:09,070 --> 00:03:16,570 And still, every concatenation keeps copying it to a new, huge part of memory, adding some tiny part 43 00:03:16,570 --> 00:03:21,250 and then replacing the reference stored in the original variable. 44 00:03:21,610 --> 00:03:27,910 Not to mention that behind the scenes, the garbage collector is struggling to clean up all those large 45 00:03:27,910 --> 00:03:30,160 but unused strings from memory. 46 00:03:30,610 --> 00:03:35,140 And for such use cases, the string builder class has been created. 47 00:03:35,770 --> 00:03:40,600 The string builder can add or remove pieces from the final result. 48 00:03:40,810 --> 00:03:48,310 But we folded these laborious copying of the old string, adding apart and removing the old string string 49 00:03:48,310 --> 00:03:53,320 builder object maintains a buffer to accommodate expansions to this string. 50 00:03:53,830 --> 00:03:58,330 New data is appended to the buffer if there is any space in it left. 51 00:03:58,690 --> 00:04:02,110 Otherwise, a new Rajo buffer is allocated. 52 00:04:02,470 --> 00:04:07,930 Data from the original buffer is copied to the new buffer, and the new data is then appended. 53 00:04:08,380 --> 00:04:13,300 As you can see, the only scenario when the entire result is being copied is one. 54 00:04:13,300 --> 00:04:15,190 The buffer needs to be enlarged. 55 00:04:15,670 --> 00:04:21,790 All right, let's see a simple program that will measure the performance of string and string builder. 56 00:04:23,490 --> 00:04:30,330 Those two methods seem to build a string of letters, a of the length given in the parameter. 57 00:04:30,900 --> 00:04:37,230 The only difference is that this one is using a string and this one the string builder class. 58 00:04:37,920 --> 00:04:42,060 Let's see how they will handle 100000 iterations. 59 00:04:45,970 --> 00:04:53,470 Well, string builder gives the result over five thousand five hundred times faster than string. 60 00:04:55,560 --> 00:05:02,010 I hope I convinced you that when you implement some process of incremental building of things, the 61 00:05:02,010 --> 00:05:04,830 string builder should be the cloth that you use. 62 00:05:05,370 --> 00:05:12,120 Of course, for simple uses like concatenating a couple of strings, using the string builder is an 63 00:05:12,120 --> 00:05:15,360 overkill and it only complicates the code. 64 00:05:15,840 --> 00:05:20,910 And if you wanted to have at least a tiny performance boost, I must disappoint you. 65 00:05:22,690 --> 00:05:30,130 This drink is not built incrementally, but is composed in a single instruction plain old string addition 66 00:05:30,130 --> 00:05:34,810 actually works faster if you try to do the same with the string builder. 67 00:05:35,080 --> 00:05:36,910 This is how it would look like. 68 00:05:38,170 --> 00:05:45,520 Actually, this code is not only much prettier and simpler than this one, but it actually works faster. 69 00:05:46,060 --> 00:05:52,270 The performance of this code is better because behind the scenes, it gets translated into this. 70 00:05:54,830 --> 00:05:56,870 Which is actually quite efficient. 71 00:05:57,200 --> 00:06:02,330 Remember, this can only be done if concatenation happens in a single instruction. 72 00:06:02,510 --> 00:06:08,390 So it would not work if, for example, we used a loop to build a string from pieces. 73 00:06:09,230 --> 00:06:09,920 All right. 74 00:06:10,220 --> 00:06:16,880 The most important thing that we need to remember from this lecture is to use string builder one incrementally 75 00:06:16,880 --> 00:06:22,820 building cloud strings as it gives much better performance than using a simple string. 76 00:06:23,540 --> 00:06:28,940 During the interview, you can be asked What does it mean that string is immutable? 77 00:06:29,690 --> 00:06:32,210 It means that one string is created. 78 00:06:32,330 --> 00:06:35,930 It can't be modified when we modify a string. 79 00:06:36,110 --> 00:06:43,130 Actually, a brand new string is created and the variable that was storing it, we simply hold a reference 80 00:06:43,250 --> 00:06:44,450 to this new object. 81 00:06:45,290 --> 00:06:46,010 Oh, right. 82 00:06:46,490 --> 00:06:50,060 That's it in this video and in the subject of strings. 83 00:06:50,420 --> 00:06:53,060 Thanks for watching and see you in the next one.