Java generics ❓

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's going on everybody it's bro here hope you're doing well and in this video i'm going to be discussing with you guys generic methods and generic classes in java so sit back relax and enjoy the show if you find this video helpful please remember to like comment and subscribe your support will help keep this channel running alright guys let's talk about generics so the definition for this is that generics enable types which are classes and interfaces to be parameters when defining classes interfaces and methods so a benefit of this is that it eliminates the need to create multiple versions of methods or classes for various data types so basically we can use one version of a method or a class for all reference data types so let me give you an example so here's an example for you guys i have four different arrays here they all have different data types and please note that these are reference data types so we have an array of integers doubles characters and strings so we're using the wrapper class for some of these primitive values such as like integer double and character so what if we wanted to display all of the elements in each of these arrays so normally what we would have to do is to create a method that can handle all of these individual different data types so let's say for example we have a method called display array and this accepts an integer array so this is only compatible with our array of integers that for our example we just called int array now if we wanted a similar method for doubles characters and strings we would have to create another method that can handle each of these different data types that we have so we'll create another array this one will accept doubles so replace integer with double and let's do the same thing for character and string so character character and then string and then let's just test this so we're going to call the display array method and let's send in our integer array then we're going to do the same thing for double array character array and string and let's run this so yes this does work but it's kind of inconvenient to have four separate methods when they basically do the same thing so here's a crazy idea how about we don't let's create one version of this method that can handle all reference data types so let's delete three out of these four methods that we painstakingly created because we only are going to need one really so in order to use generics we're going to be discussing both generic methods and then generic classes so in order to use a generic method right before the return type we're going to add a set of angle brackets and then typically if people want to use generics the common thing to do is just to place the letter t within here that's just common practice but i like to type in thing just because it helps me remember that this is a generic so this is now a generic method however we can still only pass in an array of integers so with the data type we're going to replace that with whatever value or text is within these angle brackets so we're going to accept an array of things and then we're going to replace integer here as well with thing or whatever value you placed here and remember it's usually t that people write here that's just my quirk where i like to write things just because it helps me remember this and if we were to run this this does the exact same process as what we had previously before we used the generic method so this is a i would say a shorthand or more efficient way to write your code because you don't necessarily need an individual method for each individual data type that you have you can just have one version that will accept all reference data types although you do have the capability of limiting the data types that are entering into a generic method or class by a concept called bounded parameters but we'll discuss that later on in this video so let's create another generic method but this time it's also going to return something that's generic so it's going to be a similar process to what we have set up previously with this generic method so we're going to type in public static and then this is going to be a generic method so we're going to type in a set of angle brackets and then people usually write t here but i like to write thing but you do you i guess and then with the return type this cannot be void because we're returning something so we're going to be returning a thing or whatever text you have within these angle brackets and then let's name this method let's return the first element within each of these arrays so let's call this get first for get first element i guess and then we're going to pass in an array of things so what we're going to be returning is return our array that we receive with this parameter at index 0 so it's the first element and then let's run this so i'm going to turn these into a comment for now so we will system.out.printline for each of these and then we're going to call the get first method within the print line statement so get first and we're going to pass in our different arrays that we have so get first into ray double array character array and string array and as you can see this works just fine here's a more practical scenario so figuratively speaking let's say that we're creating a video game and we have various game sprites that we want to draw on the screen we have a player an enemy an item and a tree and we want to display or draw these all within our game so this really isn't set up you can see that i've yet to create these classes but this will work just for our scenario so instead of creating a draw method that can only draw players another draw method that can only draw enemies another for items and trees so on and so forth what we would like is just a generic method it will accept a reference data type or a thing and just draw it so you can just have one method that will accept all data types because we really don't want to have an individual method for all of these separate data types we want just one method that will take care of everything for us so that's the real benefit of generic methods let's move on to generic classes okay guys let's discuss generic classes so for this example i have a few instances of some classes that we're going to create in just a moment and each of these classes is going to store a different value and these all have different data types so my int will store an integer and we're just going to pass in one to the constructor my double will pass in maybe 3.14 character the at symbol and then my string will just pass in the word hello so let's create these actual classes and then we're going to create a generic class just to compare and contrast the differences between them so let's create my integer class first so file new class we'll call this my integer class and all this is going to do is store a single integer value and we'll call it x and let's create the constructor for this class so my integer class and we're going to receive one argument and that is going to be the value integer x and we're going to assign this x equals whatever x that we receive and then let's create a getter method so public and we're not going to type in void we're going to return an integer public integer get value and we're going to return x then let's do the same thing for double character and straight so i'm just going to fast forward the video at this point all right well welcome back so we have four different classes one for my integer class my double class my character class and my string class and they all will hold one value of each of their respective types now let's say that we want to display all these values so we're going to use the first myint dot get value function and then let's put this within a print line statement so system.out.printline and then within the parentheses of this print line statement myint.getvalue and let's do the same thing for my double my character and my string so my double my character and my string so this will display all of the values that we have that we sent to the constructor for each of their respective classes now let's do the same thing again but this time instead of creating four different classes we're going to create one generic class that can hold all of these different data types so i'm actually going to delete all these i know it was a lot of work to actually build these but hey it's for the video so let's get rid of these and this time we're going to create a generic class that will function much the same so file new class we'll call this my generic class finish so to make a class generic after the class name we're going to add the angle brackets and then people usually write t within here but i like to write thing like i've mentioned before so now this is a generic class so now instead of specifying a specific data type that we want to store we're going to store a thing so we have a variable that has a thing data type and then let's create the constructor for this class so my generic class and the parameter is that it's going to accept a thing called x and this x equals x then let's work on the getter method for this and this was public thing because we're returning a thing and it is called i already forgot get value get value return x and now what we're going to need to do is change integer double character and string all to generic to match my generic class so let's just fast forward through this so these should all say my generic class now and you can see that these are all underlined and let's take a look so it says that there's a warning here that the constructor my generic class belongs to raw type my generic class references to generic type my generic class then it has the angle brackets with our thing should be parameterized so what this is telling us is that after these class definitions we should add the angle brackets and then list the data type of what we're sending to my generic class so with this number this integer one we should list that we're sending this class my generic class and integer and we're going to do the same thing with double character and string so double character and string and then just to get rid of this warning we're going to add a empty set of angle brackets after this part of the definition all right so this will work the same as it has before where all of these values are displaying and we only had to do this with one class one generic class so with these instances of our generic class you may have noticed that these are very similar to what we do to declare and instantiate an arraylist so just as a refresher we create an arraylist by typing in array list then a set of angle brackets and the data type of what we're storing within this array list and this has to be a reference data type so let's store an arraylist of strings and this will be called my friends and this will currently be empty so my friends equals new array list angle brackets parentheses and then we just need to import this so you can see that the way that we instantiated this array list is very similar to what we did to create instances of our generic class we list the class name the data type within angle brackets of what we're storing we need a name equals new then the data type again and then let's take a look at arraylist so here's the class definition for our arraylist and after the class name you can see that we have the parameter for our generic class the angle brackets and for this one it just says e but it really doesn't matter what you type within the angle brackets so that's why with array lists they're using a generic class so we can store different reference data types so we can store strings integers or even objects it really doesn't matter since this is a generic class we don't need to find a specific array list that can hold a singular data type like for example we don't need to find a particular array list that holds integers then if we want to store doubles we don't need a different array arraylist this arraylist is a generic class so we can store whatever reference data type that we want within it we just have to declare what we're going to store within it so one thing you should be aware of is that sometimes with generic classes there can be multiple parameters so for example between the angle brackets you might see each value separated with a comma and the second value is usually named v as a common convention but i'll just name this thing two just for learning purposes so my generic class now has two generic parameters and we can no longer create instances of this class only by passing in a single value so we also need to list the second value or the second reference data type that we're sending to these instances when we construct them so let's say that to create this instance of my generic class we're going to pass in an integer as well as another integer and let's pass in maybe two doubles two characters and then let's mix up the next one let's say we're going to pass in a string as well as a character so we're going to set up the constructor to accept a thing to value or reference data type so thing two and we'll call this y and let's set up the constructor so thing two will be y so this y equals y and let's return y this time and we're going to change thing to thing two so now we also need to send in another value of the matching reference data type that we have so maybe i'll also send in 9 1.01 maybe the money sign for the character so for this one this requires a string and a character so i need to send in a character this time so maybe the exclamation point and then let's run this so it looks like this works just fine we only wanted to return whatever our y value was and that is the second value that we passed into our constructors for my generic class so with our generic classes that have two parameters this is very similar to our lesson on hashmaps so a hashmap is a collection of key value pairs and these accept reference data types so let's say for example we have a hashmap of different users for our website so we can store maybe an integer this could be the user id and their string which could be their username so this is very similar to how we created our generic classes that have two generic parameters this time and let's take a look at our hash map class so this is going to be very similar to our array list except here that it has two parameters k for probably key and then v for value but like i said before it really doesn't matter what you type in here like arraylist hat e and i think that's for elements but yeah you can see that hash maps also have two generic parameters same thing with how we set up our my generic class so one last thing i'm going to introduce for this video is the concept of bounded types and that allows us to limit the scope of reference data types that we can send to a generic class so my last topic for this video is to discuss bounded types so this allows you to create objects of a generic class to have data of specified derived types so for example let's take numbers so with my generic class let's say that we only want to pass in numbers integers and doubles are fine but we don't want to send in any characters strings or anything else like that so what we're going to do after let's say thing we're going to have this extends the number class so we can send in any reference data types but it has to be a subclass of the number class so here's a list for you so here's some documentation for the number class we have thing extends the number class so we can pass in any reference data type as long as it's one of these subclasses and many of these you probably won't recognize like what the hell is an atomic lung but you can see here that we have the double class as well as integer and you probably know of float and long as well even though we really don't use them too often since thing extends number you can say that the scope of the reference data types that we can pass in are limited to just this list any class that is a subclass of the number class if we had thing extends a different parent class we would be limited in our scope to a possible different set of subclasses so that's why with our program we are limited right now to sending in integers doubles and a few other things from that list like you can see we can no longer send in characters and strings for the first value so if we had thing two also extends the number class we need to send in two reference data types that have number as a parent class so these first two lines would work just fine but we can no longer send in two characters or a string and a character value so let's just get rid of these for our example and then let's get rid of these as well and then if we were to run this this will return the second value for our instances of our generic class so that's pretty much it for this lesson hopefully your brain didn't explode from all of the information and be sure to hit that like button before your brain does explode so yeah if you'd like a copy of everything that we've done i'll post everything in the comments down below but yeah that's the basics of generics in java hey you yeah i'm talking to you if you learn something new then you can help me help you in three easy steps by smashing that like button drop a comment down below and subscribe if you'd like to become a fellow bro you
Info
Channel: Bro Code
Views: 16,195
Rating: undefined out of 5
Keywords: Java generic methods, Java generic classes, Java bounded types, Java generics, Java, generic, methods, classes, Java generics tutorial
Id: jUcAyZ5OUm0
Channel Id: undefined
Length: 22min 3sec (1323 seconds)
Published: Sun Jul 26 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.