Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
In this video we'll learn more about generics and implement our first type parameter!
This video doesn't have any notes.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
In the last video,
we added oranges to our boxOfMilk, and
0:00
didn't realize we had a problem
until we ran the app.
0:03
Not good.
0:07
Ideally, this error would be caught
much sooner by the compiler or
0:08
even by IntelliJ.
0:11
And it would show us
the error on lines 12 and 13.
0:12
To make it show us the error,
0:15
we need a way to designate each of our
boxes as only allowing a certain type.
0:17
Which we can do with generics.
0:23
Let's head over to Box.java and
use our first generic.
0:26
At the top of the class,
directly after the word Box,
0:31
let's type a less than sign, which
will automatically add its other half.
0:34
And in the middle let's type the letter T.
0:40
This is a generic.
0:44
And between these two angle brackets,
we can declare type parameters.
0:45
Let's see how it works by
going back to Main.java.
0:50
Now, when we create our boxOfMilk,
instead of just using a box variable,
0:55
we can go further and specify our type
parameter by using angle brackets.
1:01
And we'll want to do the same
thing on the right side.
1:09
Let's also update our boxOfOranges.
1:15
So right after the word Box,
1:19
we'll add the angle brackets and
say that this Box is a boxOfOranges.
1:21
Also, if you've already declared your
type arguments on the left side,
1:29
you're allowed to omit
them on the right side.
1:33
Let's use Alt+Enter to do that.
1:36
Okay, so we specified types for boxes,
but we're still not getting any errors.
1:43
Let's head back to Box.java and
see what's going on.
1:48
To recap,
this T variable is a type parameter.
1:52
It's exactly like how
a function has parameters,
1:56
just instead of passing in values,
we're passing in types.
1:59
And also just like a function, we can
name our parameters whatever we want.
2:03
So this T is just what's typically used.
2:07
We could call our type parameter Tree and
that would be just fine.
2:11
Also, just like a function
can have multiple parameters,
2:16
we can have multiple type parameters.
2:20
So if we wanted to have T, and X,
and Y, and Z, that's no problem.
2:23
Okay, let's get back to just our
one type parameter, named T.
2:29
The reason we didn't see an error
after updating our box types
2:36
is because we're not doing
anything with those types.
2:39
We've got our type parameter,
but we're not using it.
2:42
So let's start using it.
2:46
Since we want the contents of the box
to be of type T instead of type Object,
2:49
let's replace all of the references
to Object with our type parameter.
2:54
So we've got one, two, three, four.
2:59
So T and T, and T,
3:07
and T, perfect.
3:11
Now back in Main.java, we see the errors.
3:16
Awesome, getting notified immediately
that you've made a mistake
3:21
is much better than getting
an error at a runtime.
3:25
Let's go ahead and switch those back.
3:28
And then let's use Alt+Enter to clean up
these last two lines that no longer need
3:36
their casts.
3:40
Thanks to generics,
we can not only make our code more safe,
3:48
we can make it more readable.
3:52
Being able to pass around type
parameters is a pretty powerful concept.
3:55
But it's not limited to just classes.
3:59
In the next video, we'll see how to
add type parameters to a method.
4:01
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up