Let's start with a simple example: suppose we need a List dedicated to store strings, how can we implement it? Well, this is not simple, and you can see the following code:
Public static void main (String [] args) {List strList = new ArrayList (); strList. add ("one"); strList. add ("two ");}
I believe that many people have such writing experiences. It's good. This writing method is indeed correct. We did this even before. But isn't it about keeping pace with the times? This is a problem today. The problem comes from our requirements. What we need is a List specifically used to store strings. Please add a sentence after this code to make it like this:
Public static void main (String [] args) {List strList = new ArrayList (); strList. add ("one"); strList. add ("two"); strList. add (new Integer (1 ));}
Run and check. You can run it! It's good news. Don't be happy. The problem is that this List can run normally (remember: What programmers want is not that the program can run ). Looking back, our requirement is that this List is used to store strings, but now, this List can still store an integer data. Maybe you will say that the Java Class Library does not define that it can store any Object? Yes, that's right, but now our demand scope is reduced, and we only need to store strings. Let alone I am the best of the world. Let's look at a more common problem: when we operate databases, we often return all the data in a table. After we map through the object relationship, we get a series of data of the same type. Generally, we use a List to store this series of data. However, because these data have the same type, this List becomes a List that saves a single object. Here, I think we should be clear about the purpose of this example.
Now let's discuss the solution to the problem. In a word, we need a List that can only operate on a single type. The method is here. Java 5 provides us with a way to understand the problem ----- Generic. For our example, Java 5's type security has to play a role.
Now let's go back to program list 1, open your development tool, compile it (Eclipse and other tools can be seen without your own variation), and we find that the program has no errors, however, there is a warning:
Type safety: The method add (Object) belongs to the raw type List. References to generic type ListShocould be parameterized
Have you seen this? JavaTiger warned you that the source of the warning is the type security mentioned above. If you query Javadoc, you will find that the List is defined as follows:
Public interface List <E>Extends Collection, Iterable
Note this <E>It is the type security mark of JavaTiger. Here I think we can show how to define the List in our example to ensure the type security:
List <String>StrList = new ArrayList <String>();
Remember this statement before you understand it. In the future, this is the official List statement (optional _^ ). Now, after List, we have <String>In JavaTiger, this is a type-safe definition method. Combined with this statement, we can easily see that we have defined a List to store the String type. To see the effect of the definition: