课程: Java: Advanced Concepts for High-Performance Development

Introduction to generics in Java

- [Instructor] Generics make your code safer and clearer to read. You might have used them quite a lot already, maybe without even realizing. But understanding how they work and knowing some of the more advanced use cases, will help you write cleaner code. So to show why generics are needed let's have a look at an example. In this generics example class, I have a main method and inside the main method I'm creating a new array list called shapes and I'm then adding the string circle to that array list. So far this code works fine and I can also get that first element from the array list. So if I do system.out.println and then pass in shapes.gets and then pass in 0 to get that first element, that will get me that first string from the list. So to show it's working, let's run this now. So I'm going to click on the green arrow next to the main method and choose Run. And in the terminal, I can see it prints out the word circle as expected. But what if I want to assign a variable to that first value from the list? So I'm going to add another new line and do String circle = shapes.gets and then pass in 0 to get that first element. And I can see that straightaway I get a compiler error. This is because the compiler doesn't know that this object is a string. So to make it work I need to cast it. So before the quarter shapes.gets in a pair of brackets I'm going to put the word string. And that tells the compiler that this object is a string. So this now compiles and will run fine. And it might not seem like a big deal having to cast it to a string here. But if you have a really big program where this happens lots of times and at different places in your code, it can become a big hassle. And it's also not very readable. It's also not very safe. So if I look at this line where I'm creating the new array list, it's not at all obvious that this list should contain strings. So someone else could come along later and add any different type that they'd like to this list. So let's give that a go. So I'm going to add another new line and I'm going to do shapes.ads. And instead of passing in a string I'm going to pass in a rectangle type object. So I'm going to do new Rectangle. So this compiles fine as well. And I can also create a new rectangle object and assign it to that second value in the list. So I'm going to do Rectangle rectangle equals shapes.get and then pass in 1 to get that second element. And again, I'll need to cast it. So before shapes.get in a pair of brackets, I'm going to put the type rectangle. So this list could contain all different types of objects and that makes it really difficult to work with. And it also means it's easy to get it wrong when you're casting the objects. For example, I could tell the compiler that actually instead of a rectangle, this is an integer. So if I change rectangle to integer, and cast it to an integer, this also compiles fine. Even though I know that I've added a rectangle object rather than an integer. I won't know there's anything wrong until I go to run this. So if I run it again, by clicking on this green arrow again, at this point I get a class cast exception because I'm saying it's an integer even though it's actually a rectangle. The solution to all of these problems is generics. So all I need to do is after the word list when I declare my list, is put a pair of angle brackets and inside the angle brackets I just need to put the type that I want this list to contain. So in this case, I want it to contain strings. So I'm going to put the word string. I'm also going to put a pair of angle brackets after the arrays on the right-hand side. But I don't need to put the type inside these ones because the compiler can infer from the left-hand side that this should contain strings. So now I do get a compiler error when I try and add my rectangle object. So to get this to work, I need to pass in a string. So I'm going to change this to a string with the word rectangle in it. And I can now get rid of all the places where I'm using my cast. So I'm going to get rid of the cast in front of shapes.get 0 and the caste integer in front of shapes.get 1. And I'm going to change this value from integer to string. So generics are a powerful parts of the Java language which make our codes safer and easier for us to write and also easier for other people to read.

内容