So we're now gonna start talking about generics.
So what are generics in Java and why would we want to actually use them?
And actually if you'll see in a minute we've already used generics so
far in the course, and they actually are incredibly useful.
So just as we can create methods that take arguments in Java and
we can replace the parameters that we declared from the method.
Generics allow us to create classes in the faces of methods that take types of
parameters called unsurprisingly type parameters.
So different to other parameters we're talking about type parameters.
So you've seen in previous videos, when we created array list and
also link lists, this in use.
But just to show how useful they are,
we're gonna start with an array list example that doesn't use generics.
So we're gonna create an array list populated with integers.
And a method that prints the context of the array list doubled.
So let's go through and write that code, so
good idea to see that first before we actually go ahead and use generics.
So go to top ArrayList items = new ArrayList,
we'll add some items.
Items.add one.
Let's add another four so 2, 3, 4, 5.
So that's actually adding an integer to the array list and
it's using auto boxing automatically which we've done before in the course.
Is converting the primitive type that we've just typed in there into an integer.
And what we're going to do then is call a method called printDoubled.
We're gonna pass the parameter which is our ArrayList items and that
function we're going to right now and it's purpose is just to double the contents.
So in the top private_static void printDoubled and
it's gonna be an ArrayList n and
we'll put for(Object i : n ).
Then we're gonna print Integer, I'm casting it as an integer i * 2.
So just before we actually discuss that we'll actually run it to make sure that it
actually works and you can see we've got the numbers 1, 2, 3, 4, 5 and
we're actually doubling them multiplying the current value by 2.
So we're getting 2, 4, 6, 8, 10, so that actually works.
We get the expected output.
But note however, we actually used object here.
Cuz we didn't actually specify what the object was above,
and we also had to cast it as an integer.
So if I actually got rid of the cast here, we'll actually get an error and
the error was cannot be applied to object.
So we have to sort of tell Java, what type of object is actually in that ArrayList.
So I'll put that back so the code doesn't break.
But technically, the ArrayLlist as we've created it, can actually contain anything.
Any type of object, not just an integer which we've basically done here.
So that's actually more than a minor inconvenience,
because it also completely breaks the compiler's type checking.
And just to see the impact of how this will affect us,
if I try to add something here like Items.add and add my name there.
Now that's quite valid and there's no indication that we've actually got
a problem with our code there's no red indications or red underlining in our code
as far as Java is concerned this is valid and it actually is valid.
But the problem is when we run it, we actually get an exception.
So what's actually happening is with the object which was a string Tim in this
case, trying to be converted to an integer and it cannot be actually converted.
You can see that cannot,
java.link dot string cannot be cast to java.link.integers.
So in other words, we actually got an error because I was trying to add
a different type of data to the array list.
The real problem wasn't so much so crashed although that's bad enough, but
the real problem is that it's not actually showing up until run time.
So in other words, the program compiles fine, I can come here and build and
rebuild a project and as far as Java is concerned there are literally no problems.
We got a couple of warnings over here, potential warnings.
Uncheck call to at ease remember those raw taught array list which is an intelligized
way of sort of saying that maybe you should be doing this a different way.
But the point is we don't actually get any errors as such.
So if you actually see these errors in more detail,
we can come up to the View menu click Tool Window.
And actually select Messages, so if you click on that or we could do Cmd+0, and
you can probably see what the equivalent key stroke is in your operating system,
so click on that.
You can see it's Alt+0 for Windows Linux.
And we can actually see those warnings in more detail.
You can actually see that main.java uses unchecked or unsafe operations, and
that's Java's way of actually saying that there's actually a problem with our code.
And again, just a confirmation that if we actually haul out this over here IntelliJ
is also sort of saying okay, now what are you doing here?
So basically the Java compiler and obviously IntelliJ by default knows how to
provide these warnings because they're actually looking at your code and
they know that array list is a generic type.
But we're using it without specifying a type parameter.
In other words using it without generics.
So in other words, what we haven't done is sort of said with our ArrayList
what type of objects we're going to be storing in the ArrayList.
So the ArrayList as we've used it here is a raw type.
Now prior to Java 1.5, this was the only way to code, there was no generics.
And when Java 1.5 come out,
they actually introduced the generics which we're about to talk about.
And they actually left this old way of doing things in purely for
backwards compatibility with code that's written from previous versions.
So in other words,
if they didn't do that, that old code would no longer run any more.
So the old code continues to run but when you're writing new code,
you should almost never use these raw types any more.
And you can see why, because obviously in this instance,
we were able to crash it quite easily by making a simple little change there.
And you might look at that and think well hang on, I know that there's integers and
I'm never gonna actually add a string there.
But thinking ahead down the track,
when your programs start getting a lot more complex.
It can be a lot harder to track these types of bugs.
This one is quite easy cuz it's only a few line of code.
It's certainly not recommended to use these raw types, and because type safety
is so easy to implement these days in the current versions of Java,
I highly recommend you actually do that.
So when we actually provide a type parameter to a generic type,
this is called a parameterized type.
So I've actually specified the type using angle brackets,
and I know you've seen this before in earlier videos.
So what we're gonna do now is we're gonna specify an integer type for me clear
the ArrayList on line 8, and also in the parameter on printDouble on line 20.
So I've come up here to line eight, we're gonna actually change this.
We put our angle brackets in integer.
So we're actually saying,
now this is the type that we're actually gonna be using in this ArrayList.
We're also gonna come down here to line 20, to line 19 I should say, and
put ArrayList Integer like so.
So we're now specifying, this is the type of data that we're actually going to be
saving in our ArrayList.
And you can see what's happened straight away,
that there's still a couple of errors.
And you can see this one here's actually an error.
It's actually telling us now that you're trying to add a string
to what if defined as an ArrayList of integers.
And it's actually an error it's not a warning anymore, it's actually an error.
So it's actually not allowing us to do that.
But the first warning, it's still saying its an unchecked assignment.
It's actually saying Java.link.integer, but
it's still as far as it's concerned, an unchecked assignment.
If I press my control function F1, and
you can see it's giving us this as an example of that.
We can actually try and fix that by putting another assignment here,
if you type integer as well there.
And that actually does get rid of that warning.
So it does actually pay to look at these warnings that actually come up and
we're appropriate to actually remove the code
that is actually causing the warnings you see in this case now,
by adding the integer twice we'll actually remove the warnings.
And I can actually now get rid of this as well, cuz that's no longer relevant.
And we've now got a tick in the top right hand corner, and
that basically says as far as IntelliJ is concerned,
there's no longer any errors in our code and our code is good to go.
And again, if I git rid of that parametrize type,
we actually do get some warnings again.
We get a few warnings one here, and also one here as well.
So do actually pay heed to those errors, the warnings,
because they do actually make sense.
So check them to make sure your code's adhering to the standards and
you're not actually basically writing bad Java code.
So adding a type hue is actually unnecessary because Java's got enough
information in the declaration to the left hand side to actually include that.
So it's really to see whether you're happy with these warnings or
not you could quite happily as I've showed you delete that.
And that is still valid and the code will still work you've just got some warnings.
So generally speaking,
I would recommend just to do whatever it takes to get rid of the warnings and that
you're actually doing things the right way as far as just the Java's concerned.
And also a note here if you're actually doing this,
this is another way of doing it.
You can actually just specify the angle brackets without a type.
And we've actually got a error message and
that's probably cuz I haven't set my appropriate language level up.
So I'm just gonna go into my settings,
File > Project Structure and I've got this set to six instead of eight.
So I'm gonna set that to eight.
You notice diamonds is set for version seven, but I'm gonna set it to version
eight cuz that's what it should be for each project anyway.
If I click OK to that, that error should actually go away.
And so what's that saying there now,
that's probably the best way of actually writing the code
because what that is saying is that the fist declaration here in the left hand
side is all that it needs and we don't actually have to type another one.
And I think we'll actually get a warning now, if they actually type this in here,
cuz we're actually set to 1.8.
You can see we actually got a warning now.
Explicit top argument can be replaced with the diamond, and that's why this isn't to
a less than and a greater than sign together is referred to as a diamond.
So that using the version 1.8.
And again, I did that with File > Project Structure,
just making sure project language level was set to 1.8.
Because I've got that set to 1.8, we've actually got a tick across the board.
So that's actually the correct way to actually use it to make sure you haven't
got any warnings, but if you were using an older level of source code as you saw I
had 1.6 in use, then you will have to specify the actual parameter there.
And again, using that without
the parameter in the middle is called the diamond.
So now, we haven't got any errors and there's no warnings left,
and I actually remove this because of course before that removing that
we've actually got one error and of course the error again,
was trying to add string to add integer object to an array list of integers.
So I'm gonna comment that out to get rid of that error.
So we've now got no warnings and no errors.
We've got the green tick in the top right hand corner and
that's exactly where you wanted.
So by telling array list what types of objects we wanted it to hold,
the program now fails to compile when we try to add the value item tim.
So I had to comment that out in order for the code to even be able to work.
Now incidentally, coming down here to the loop.
We can actually get rid of this cast now, we don't need that in there anymore.
We can change this, we can either set that to be an integer or
we can actually even go back and set that to an int.
And Java is actually handling that for us automatically.
Obviously, we don't need the object now,
because we know what kind of data it actually is.
And Java is smart enough to figure out the locate you using an array list of
integers here.
Therefore, I can I know how to convert it back to an int and
it's doing that automatically with the auto boxing and
unboxing that we've talked about in previous videos.
And if we actually run this code we should find that it actually works and
you can see we didn't get the same result.
So that's the introduction to generics.
In the next video, what I'm gonna do is take it to the next level,
we're gonna actually look at creating our own generic classes.
So I will see you in that video.