1 00:00:00,093 --> 00:00:03,427 In the last video, we added oranges to our boxOfMilk, and 2 00:00:03,427 --> 00:00:07,140 didn't realize we had a problem until we ran the app. 3 00:00:07,140 --> 00:00:08,210 Not good. 4 00:00:08,210 --> 00:00:11,626 Ideally, this error would be caught much sooner by the compiler or 5 00:00:11,626 --> 00:00:12,605 even by IntelliJ. 6 00:00:12,605 --> 00:00:15,588 And it would show us the error on lines 12 and 13. 7 00:00:15,588 --> 00:00:17,896 To make it show us the error, 8 00:00:17,896 --> 00:00:23,970 we need a way to designate each of our boxes as only allowing a certain type. 9 00:00:23,970 --> 00:00:26,630 Which we can do with generics. 10 00:00:26,630 --> 00:00:29,760 Let's head over to Box.java and use our first generic. 11 00:00:31,090 --> 00:00:34,455 At the top of the class, directly after the word Box, 12 00:00:34,455 --> 00:00:40,530 let's type a less than sign, which will automatically add its other half. 13 00:00:40,530 --> 00:00:43,260 And in the middle let's type the letter T. 14 00:00:44,360 --> 00:00:45,655 This is a generic. 15 00:00:45,655 --> 00:00:50,809 And between these two angle brackets, we can declare type parameters. 16 00:00:50,809 --> 00:00:55,904 Let's see how it works by going back to Main.java. 17 00:00:55,904 --> 00:01:01,243 Now, when we create our boxOfMilk, instead of just using a box variable, 18 00:01:01,243 --> 00:01:06,522 we can go further and specify our type parameter by using angle brackets. 19 00:01:09,932 --> 00:01:12,287 And we'll want to do the same thing on the right side. 20 00:01:15,701 --> 00:01:17,995 Let's also update our boxOfOranges. 21 00:01:19,330 --> 00:01:21,214 So right after the word Box, 22 00:01:21,214 --> 00:01:25,859 we'll add the angle brackets and say that this Box is a boxOfOranges. 23 00:01:29,556 --> 00:01:33,680 Also, if you've already declared your type arguments on the left side, 24 00:01:33,680 --> 00:01:36,800 you're allowed to omit them on the right side. 25 00:01:36,800 --> 00:01:39,270 Let's use Alt+Enter to do that. 26 00:01:43,208 --> 00:01:48,985 Okay, so we specified types for boxes, but we're still not getting any errors. 27 00:01:48,985 --> 00:01:52,305 Let's head back to Box.java and see what's going on. 28 00:01:52,305 --> 00:01:56,480 To recap, this T variable is a type parameter. 29 00:01:56,480 --> 00:01:59,220 It's exactly like how a function has parameters, 30 00:01:59,220 --> 00:02:03,520 just instead of passing in values, we're passing in types. 31 00:02:03,520 --> 00:02:07,800 And also just like a function, we can name our parameters whatever we want. 32 00:02:07,800 --> 00:02:11,480 So this T is just what's typically used. 33 00:02:11,480 --> 00:02:16,110 We could call our type parameter Tree and that would be just fine. 34 00:02:16,110 --> 00:02:20,380 Also, just like a function can have multiple parameters, 35 00:02:20,380 --> 00:02:22,250 we can have multiple type parameters. 36 00:02:23,420 --> 00:02:29,620 So if we wanted to have T, and X, and Y, and Z, that's no problem. 37 00:02:29,620 --> 00:02:33,400 Okay, let's get back to just our one type parameter, named T. 38 00:02:36,160 --> 00:02:39,770 The reason we didn't see an error after updating our box types 39 00:02:39,770 --> 00:02:42,730 is because we're not doing anything with those types. 40 00:02:42,730 --> 00:02:46,660 We've got our type parameter, but we're not using it. 41 00:02:46,660 --> 00:02:48,430 So let's start using it. 42 00:02:49,470 --> 00:02:54,719 Since we want the contents of the box to be of type T instead of type Object, 43 00:02:54,719 --> 00:02:59,904 let's replace all of the references to Object with our type parameter. 44 00:02:59,904 --> 00:03:05,266 So we've got one, two, three, four. 45 00:03:07,208 --> 00:03:11,963 So T and T, and T, 46 00:03:11,963 --> 00:03:16,721 and T, perfect. 47 00:03:16,721 --> 00:03:21,735 Now back in Main.java, we see the errors. 48 00:03:21,735 --> 00:03:25,879 Awesome, getting notified immediately that you've made a mistake 49 00:03:25,879 --> 00:03:28,900 is much better than getting an error at a runtime. 50 00:03:28,900 --> 00:03:29,883 Let's go ahead and switch those back. 51 00:03:36,603 --> 00:03:40,734 And then let's use Alt+Enter to clean up these last two lines that no longer need 52 00:03:40,734 --> 00:03:41,470 their casts. 53 00:03:48,875 --> 00:03:52,633 Thanks to generics, we can not only make our code more safe, 54 00:03:52,633 --> 00:03:55,130 we can make it more readable. 55 00:03:55,130 --> 00:03:59,280 Being able to pass around type parameters is a pretty powerful concept. 56 00:03:59,280 --> 00:04:01,770 But it's not limited to just classes. 57 00:04:01,770 --> 00:04:04,960 In the next video, we'll see how to add type parameters to a method.