Java Generics Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back everybody Oh actually one second all right much better what's going on everybody my name is Sam this is a keep on coding channel and today we're gonna be talking about Java generics now before throwing a bunch of definitions at you and confusing you all I think the best way to understand generics are to look at the problem that it's solving and then once we understand that we can see how generics solves that problem so first let's take a look and understand what the actual problem is so initially we just have an empty project here and say we want a for some reason we want a class that just prints out a variable so what we could do here is we can say ok new class and we'll just call it my class integers and in this class we're going to go ahead and create an integer object called I we want to first create a constructor or so we can set the value of I and secondly we want to function that prints out I so this works right but say now we want to do the same thing but we want to do it with doubles so what we could do here is you can say okay file new Java class and we'll call it my class double and then basically we can go in here we could copy everything over and we can just change everything to double okay so now say we want to do the same thing with strings so what we could do here is go file class copy that over so you guys kind of see what the problem is here is that we're creating duplicate code that basically does the same thing the only thing that changes is the variable type that we're operating on so this is where generics comes in what we can do is we can just create one class and it can take in different types of variables so to save all that duplicate code what we can do here is we can just close out these classes and we can just create one class and we'll call it my class so how would these syntax for this actually look like so what happens is we go in between the end of the class name and the first curly brace and we make these angle brackets and we type in you can really use whatever as long as it's not a keyword but generally the normal or the practice is to use the letter T now instead of creating you know as an integer or double what we do is we just say T and then we'll just call it something like object or OB now when we go in and we want to initialize an instance of this class we would go my class and then we'd use angle brackets to specify what we actually want to pass in so say for example right now we want an integer okay and then we'll just say object equals new my class okay cool so say we want a double now so all we need to do is if we want to create a new instance of that we'll call it object two and all we do is we just change this from an integer to a double and as you can see we don't get any errors or anything so let's go back and add some stuff to our my class so let's first go ahead and create a construct here so again since we don't know what type of variable we want to pass in to initialize our OB again we could just do t OB and then we can set our classes it will be equal to what gets passed in in this constructor and let's go ahead and create another method that prints out the actual type of our OB variable so let's go ahead and create another function here that prints out the type of our variable and we can do that by saying OB get class get name so if we go back to main we now have to pass in a variable to our constructor so for integer let's just pass in 10 for double let's go ahead and pass in 20 now let's go ahead and call obj so type and obj to dot so type so if we go ahead and run that we see that the first variable is an integer and the second variable is a double which is what we were expecting one thing to note is that what gets passed in into my class as a generic needs to be an object type it can't be a primitive so you can't do something like my class int and if we look at the error it's saying type argument cannot be a primitive type so you can't use something like int you can't do something like the lowercase double that's gonna give you an error as well so it has to be it has to be an object not a primitive type another thing to know which was what we actually already did is that you can pass in generics as parameters so it doesn't just have to be a class member you can also pass them in as method arguments so another thing you might be asking is okay this is cool and all but what if what if what if I go back to my class and I have more than one variable how do i how do I handle that well I got the solution for you guys basically all you need to do is you just add a comma and you add another variable type so we could pass something like V and you can have really as many of these as you want so for this example I'm just going to have to what we would do is we would just create another variable and we can also initialize that in the constructor and we can also show the type of that variable so now it's giving us an error because it's expecting two variables now so let's go ahead and pass an integer as well as a double and let's go ahead and run that and as we see down here we get integer as well as double because if we go back here we see that it's printing out the class of OB as well as the class of OB two so this would be useful in something like a hash map say you want to create a hash map here if you see that a lot it allows us to pass it in at T and that V and if we actually go to the definition of Java implements hash map we see that it calls public class hash map and it passes in two generics as the key and the value so as you can see this is something that Java developers use to create Java classes so as we can see Java generics allow us to utilize code reusability basically we don't need to create a new class for every variable type that we need to use so now that we have the basics of generics down let's go ahead and look at something a little bit more advanced all right so let's go back to our main class let's go ahead and delete everything and let's create a new class and let's call it number functions so in this class say we're building out just a bunch of different functions that we can perform on numbers so we need to do the same thing again where we have that diamond notation where we add a T and then we have our T object and then again we're going to create a constructor to initialize our object and say at this point we want to do something like get the square of a number [Music] so as you can see here we're trying to multiply object times object but we're getting this error here that says operator star cannot be applied to TT this is because this multiplication can only be applied to numeric values right but if we have this T like what happens if we pass in a string how is it going to multiply that string so Java knows this and it's basically saying that allowing you not to do something like this so how can we ensure that what we pass in as our generic is an actual number well what generics provide is something called bounded types where you can bound the type of your generic to something and how this looks like is we would go up to here and we would say T extends number now if we go down here we do OB dot int value OB dot double value and as we see the arrow goes away so I created an object of numeric functions of type integer and I initialize it with the number four and then I called IOB dot square and if we go ahead and run that we see that it prints out 16 so how does this actually work so we have this class number here and it's children here are integer double float and basically any other numeric value so down here we're saying we could have key as long as t extends the number class so basically if it's a child of number this is okay so if we go back and we say hey we want to initialize this with a string again if you see what's gonna give us this error of type parameter string is not within its bound it should extend java.lang done number all right so we're gonna look at one last example here and that is the concept of wildcards and again we're gonna look at the problem first and we're gonna see how wildcards solves that problem so if we go back to our numeric functions class let's go ahead and just delete that there I'm gonna go ahead and change this to just because we know now we now know that this needs to be a number and I'm going to go ahead and create this new method here so what this function does is it passes in a parameter of also of type numeric functions and what it checks is that the absolute value of this number that gets passed in is the same as the number in our class so basically it checks if the absolute value of num double value equals the absolute value of the object that we passes in number dot double value and if that's true then it returns true for everything otherwise it returns false so if we go back to main we create two objects one of type integer and one of type double and we go ahead and call this absolute value method and as you can see it's giving us an error here saying that it's expecting type integer but you're giving me a type double now why is this happening well if we go back to this object here the value that we're passing in is an integer so if we go back to numeric functions for this class now T is always going to be integer so when we pass in a numeric functions object here it's also expecting that to be of type T which in this case is going to be integer but when we're passing it in here do B is of type double so that's why it's saying it's expecting an integer but it's getting a double so how do we solve this problem well what we need to do is we need to go back and say we don't want type T here we put a question mark here saying that we don't know what this type is gonna be it could be a double could be a float it could be an integer now if we go back we see that the error is gone and just for fun let's go ahead and print this out so let's go ahead and run that and the values is true which is what we were expecting all right so that is the basics of Java generics hopefully you guys got a lot of value out of this video I remember the first time seeing generics I saw these like t's and question marks and I thought it was pretty scary but once I actually learned it I was like oh this is actually not that bad and it's actually very useful so hopefully you guys feel the same way now if you guys did like the video you know make sure to smash that like button but as always thank you guys so much for watching and happy coding [Music] [Music]
Info
Channel: Keep On Coding
Views: 89,186
Rating: 4.9791417 out of 5
Keywords: java generics, java generics tutorial, java generics advanced tutorial, java 8 generics tutorial, java tutorial, java tutorial advanced
Id: h7piyWnQbZA
Channel Id: undefined
Length: 12min 23sec (743 seconds)
Published: Wed May 06 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.