1 00:00:00,300 --> 00:00:02,050 Oh, boy, the reference trap. 2 00:00:02,070 --> 00:00:05,850 This is my favorite lesson because I've seen the best developers get caught in the trap. 3 00:00:06,090 --> 00:00:10,760 That's because nobody told them about it and they had to learn the hard way by falling into the trap. 4 00:00:10,770 --> 00:00:14,130 But because you're my student, I'm going to make sure that you don't fall into any traps. 5 00:00:16,920 --> 00:00:23,130 I told you earlier that a variable cannot store an array, it stores a reference that points to it because 6 00:00:23,130 --> 00:00:27,480 of this, another variable can actually store reference that points to the same array. 7 00:00:28,440 --> 00:00:29,520 Isn't that weird? 8 00:00:29,880 --> 00:00:33,330 As far as I know, there isn't an official name to describe this phenomenon. 9 00:00:33,570 --> 00:00:35,460 So I call it the reference trap. 10 00:00:39,390 --> 00:00:43,740 In this lesson, we're going to talk about the reference drop, the violation that causes it and the 11 00:00:43,740 --> 00:00:45,180 solution that prevents it. 12 00:00:49,790 --> 00:00:54,980 First things first, inside Section six, make a new file named reference, drop Java and inside the 13 00:00:54,980 --> 00:00:57,080 class, make sure it has a main method. 14 00:01:03,740 --> 00:01:06,890 This trap happens with many variables, Cheryl, reference to one array. 15 00:01:08,400 --> 00:01:13,440 This is about and I call it the reference strap, because if I change the area through one variable, 16 00:01:13,470 --> 00:01:15,130 the other variable gets affected as well. 17 00:01:15,690 --> 00:01:19,340 In this example, I set numbers equal to an array of three values. 18 00:01:19,740 --> 00:01:23,490 But notice that I can update the array from another variable numbers to. 19 00:01:26,120 --> 00:01:27,110 This is bad. 20 00:01:27,140 --> 00:01:32,930 This is a trap, and to avoid the reference trap, do not set array variables equal to each other. 21 00:01:33,680 --> 00:01:39,020 That's because, well, suppose you have a variable that stores an array of integers, numbers, stores, 22 00:01:39,020 --> 00:01:40,870 a reference that points to the array. 23 00:01:41,210 --> 00:01:46,760 If I set another variable equal to numbers, it's going to copy what's inside and what's inside is the 24 00:01:46,760 --> 00:01:47,510 reference. 25 00:01:47,870 --> 00:01:51,260 So now both variables share a reference to the same array. 26 00:01:54,640 --> 00:01:58,780 And so the reference trap happens when we set array of variables equal to each other. 27 00:02:01,190 --> 00:02:07,340 No stores a reference that points to the array and by setting numbers to equal to numbers, it copies 28 00:02:07,340 --> 00:02:10,610 the value inside and what's inside is the reference. 29 00:02:11,060 --> 00:02:14,040 Now, both variables share a reference to the same array. 30 00:02:14,690 --> 00:02:18,380 So it's not a coincidence that number two is reference is also a pink circle. 31 00:02:27,310 --> 00:02:30,040 I want to commit this violation and show you the trappin code. 32 00:02:31,360 --> 00:02:35,810 So let's assume you're a manager at the Java Cafe and you have the staff list from last year. 33 00:02:36,340 --> 00:02:39,220 I'm going to set a variable string staff last year. 34 00:02:46,230 --> 00:02:49,260 Is equal to an array of three values, Tommy. 35 00:02:50,930 --> 00:02:52,280 Joel and Ellie. 36 00:02:55,290 --> 00:02:59,220 And now I'm going to print the contents of staff last year as one long string. 37 00:03:24,070 --> 00:03:24,430 Good. 38 00:03:24,700 --> 00:03:29,980 Nothing too hard, we're just printing an array of string values, and now this year, only one staff 39 00:03:29,980 --> 00:03:32,910 member changed and you're too lazy to rewrite another array. 40 00:03:33,370 --> 00:03:39,420 So you decide to set another array variable equal to this one string stuff this year is equal to staff 41 00:03:39,430 --> 00:03:40,000 last year. 42 00:03:45,500 --> 00:03:47,860 And you're going to print the contents of the story as well. 43 00:04:15,020 --> 00:04:16,420 OK, we're off to a good start. 44 00:04:17,810 --> 00:04:22,470 And now, unfortunately, Joel went missing in a new employee named Abby took his place. 45 00:04:23,180 --> 00:04:24,680 The staff this year changed. 46 00:04:24,690 --> 00:04:27,560 So we're going to change the next one from the second variable. 47 00:04:40,950 --> 00:04:41,970 We were on the code. 48 00:04:52,970 --> 00:04:54,020 Oh, that's not good. 49 00:04:54,440 --> 00:04:58,770 I meant the only update this year staff list, but last year, staff list updated as well. 50 00:04:59,330 --> 00:05:02,270 This is very bad because now last year's data is ruined. 51 00:05:02,810 --> 00:05:05,300 This is the reference trap I was telling you about. 52 00:05:05,840 --> 00:05:10,610 When you set a variable equal to another eight copies, the value inside, which is a reference. 53 00:05:11,790 --> 00:05:16,410 If I update this right through one variable, it will affect the other because now they both point to 54 00:05:16,410 --> 00:05:17,310 the same array. 55 00:05:17,790 --> 00:05:18,810 This is bad. 56 00:05:21,900 --> 00:05:25,860 The state of a variable should not change because you updated another. 57 00:05:27,400 --> 00:05:30,700 In a perfect world, every variable should point to its own array. 58 00:05:35,230 --> 00:05:39,460 So what's the solution if I want to copy an array into another variable, how do I do it? 59 00:05:40,330 --> 00:05:42,010 The solution is to make a new era. 60 00:05:45,810 --> 00:05:51,420 This trap occurs when you set array variables equal to each other, so don't be lazy, create a new 61 00:05:51,420 --> 00:05:55,620 array and then copy every evalu into the new array using a for loop. 62 00:05:57,830 --> 00:06:03,500 So back in our code, we're going to do just that, I'm going to set stuff this year equal to a new 63 00:06:03,500 --> 00:06:06,020 string array with a length of three elements. 64 00:06:12,070 --> 00:06:15,340 Then I'm going to create for a loop that starts at ENTI equal to zero. 65 00:06:16,790 --> 00:06:21,590 And it's going to run through every element in one of the arrays, they both have a length of three, 66 00:06:21,600 --> 00:06:23,810 so it doesn't matter which length you use. 67 00:06:25,730 --> 00:06:26,090 Plus. 68 00:06:29,360 --> 00:06:33,950 And during each run, I'm going to update each element and stuff this year by setting it equal to an 69 00:06:33,950 --> 00:06:37,070 element from staff last year with the same index. 70 00:06:42,180 --> 00:06:43,560 Now I can rerun the code. 71 00:06:52,180 --> 00:06:53,490 Okay, perfect. 72 00:06:53,530 --> 00:06:57,670 We successfully copied every element from the staff last year to the staff this year. 73 00:06:58,450 --> 00:07:01,390 Now what happens if we change the next one from the second variable? 74 00:07:06,790 --> 00:07:10,300 So I'm going to change the next one, I'm going to set that equal to Abby. 75 00:07:18,310 --> 00:07:20,830 And the world makes sense again, sweet. 76 00:07:21,770 --> 00:07:25,100 That's because you stuff that you're equal to a brand new array. 77 00:07:28,820 --> 00:07:34,700 The loot copies every value from staff last year to the same index stuff this year, and now each variable 78 00:07:34,700 --> 00:07:37,370 stores a unique reference to its own array. 79 00:07:46,370 --> 00:07:49,460 Updating the second variable should have no effect on the first. 80 00:07:54,480 --> 00:07:59,340 Now, here's a better solution, erase that copy of because for something so simple, the for loop is 81 00:07:59,340 --> 00:08:00,390 so much code. 82 00:08:01,170 --> 00:08:05,550 The erased utility class that we've been using to call the two string method has another useful method 83 00:08:05,550 --> 00:08:06,750 called copy of. 84 00:08:07,200 --> 00:08:11,800 It takes two arguments the area that you want to copy and how much of it you want to copy. 85 00:08:12,540 --> 00:08:15,450 This looks a lot easier and it seems like a much better solution. 86 00:08:15,840 --> 00:08:17,220 Someone to a better table. 87 00:08:17,220 --> 00:08:19,590 And don't worry, this will be in your cheat sheet. 88 00:08:24,170 --> 00:08:26,780 So in your code, you remove this monstrosity. 89 00:08:29,470 --> 00:08:31,750 And replace it with a raised copy of. 90 00:08:38,539 --> 00:08:44,360 And erase that copy of requires two arguments, the you want to copy stuff last year and how much of 91 00:08:44,360 --> 00:08:45,220 it you want to copy. 92 00:08:45,590 --> 00:08:47,810 We're going to copy the full length of the array. 93 00:08:55,390 --> 00:08:57,460 All right, and we get the same result. 94 00:08:59,330 --> 00:09:04,880 That's because stuff this year is equal to a copy of the old array, I left out the second argument 95 00:09:04,880 --> 00:09:07,030 in this animation just because there wasn't any room. 96 00:09:07,040 --> 00:09:08,030 But you get the point. 97 00:09:08,030 --> 00:09:09,980 Each variable points to a unique array. 98 00:09:18,300 --> 00:09:22,770 I hope now you realize that working with a raise is like playing with knives, but as long as you're 99 00:09:22,770 --> 00:09:25,220 careful and you follow my table, you should be fine. 100 00:09:29,630 --> 00:09:34,130 In this video, you learned about the reference drop, the reference trap happened when you mishandle 101 00:09:34,130 --> 00:09:37,740 the race, do not separate variables equal to each other. 102 00:09:38,210 --> 00:09:39,820 This doesn't copy the array. 103 00:09:39,830 --> 00:09:41,180 It copies the reference. 104 00:09:41,720 --> 00:09:48,080 The right way to copy arrays is to use erase that copy of it sets the variable equal to a copy of the 105 00:09:48,080 --> 00:09:49,010 original array. 106 00:09:49,400 --> 00:09:51,910 And now each variable points to a unique array. 107 00:09:52,100 --> 00:09:56,840 And you don't have to worry about the state of one variable changing because you updated another.