Java generics warning: xxis a raw type

Source: Internet
Author: User
Javatiger (java5.0)-arraylist is a raw type2008-04-08
  1. (This example applies to JDK 5.0. install and configure JDK first !!!)
  2. 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:
  3. Public static void main (string [] ARGs)
  4. {
  5. List strlist = new arraylist ();
  6. Strlist. Add ("one ");
  7. Strlist. Add ("two ");
  8. }

  9. 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:
  10. Public static void main (string [] ARGs)
  11. {
  12. List strlist = new arraylist ();
  13. Strlist. Add ("one ");
  14. Strlist. Add ("two ");
  15. Strlist. Add (New INTEGER (1 ));
  16. }
  17. 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.
  18. 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.
  19. 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:
  20. Type Safety: The method add (object) belongs to the raw type list.
  21. References to generic type list shocould be parameterized
  22. 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:
  23. Public interface list <E> extends collection, iterable
  24. Note that this <E> is the type security flag of javatiger. Here, we can show how to define the list in our example to ensure the type security:
  25. List <string> strlist = new arraylist <string> ();
  26. Remember this statement before you understand it. In the future, this is the official list statement (optional _^ ). Now let's take a look. After the list, we have the <string> flag. In javatiger, This is a type-safe definition method. Combined with this statement, we can easily see that, we define a list to store the string type. To see the effect of the definition:
  27. Public static void main (string [] ARGs)
  28. {
  29. List <string> strlist = new arraylist <string> ();
  30. Strlist. Add ("one ");
  31. Strlist. Add ("two ");
  32. }
  33. Compile it again or observe it directly in eclipse. The previous warning is absent.
  34. At this point, we should say that we have mastered the basic usage of type security. Now let me make a small change and change the definition of list to this:
  35. List <string> strlist = new arraylist ();
  36. The warning came back. Although we have defined the string type for the list here, but we did not specify it during instantiation, so this warning is prompted, so remember the official statement mentioned above.
  37. Now, let's verify whether the list that ensures the type security is safe. Let's continue through the list
    Add an integer for verification:
  38. Public static void main (string [] ARGs)
  39. {
  40. List <string> strlist = new arraylist <string> ();
  41. Strlist. Add ("one ");
  42. Strlist. Add ("two ");
  43. Strlist. Add (New INTEGER (1 ));
  44. }
  45. An error occurred. (This is not a warning !) Is it amazing? Let's take a look at the error message:
  46. The Method Add (string) in the type list is not applicable for the arguments (integer)
  47. The effect is obvious. The add method of this list can only accept parameters of the string type. The add (Object OBJ) method in javadoc is no longer available. Now everyone should be satisfied, our requirements have been met, and this list is too secure. Think about the benefits it brings. In the future, when we want to process the elements in a list, we don't need to write this again:
  48. String STR =
    (String) list. Get (I
    );
  49. With type security, we can directly write:
  50. String STR = list. Get (I );
  51. This may not satisfy you, but let's look at this method:
  52. Public list <string> getstrlist (list <string> List)
  53. {
  54. .....
  55. Return xxx;
  56. }
  57. This is the power of parameterized types. Don't tell me that you don't think this method is special.
  58. Well, I think my goal has been achieved. Everyone should understand what type of security in Java 5 is and how to use it. Well, in order to make everyone feel at the bottom, write two more lines of code for you to see:
  59. Map <integer, string> mymap = new hashmap <integer, string> ();
  60. Set <myclass> set = new hashset <myclass> (0 );
  61. No more examples. These examples are straightforward enough.

  62. At the end of this article, let's go back to program list 1. Didn't we receive a warning? If you are such a person, you do not want to use type security to eliminate warnings, and do not want to see the nasty waining, you can add this sentence before the main method (if you use IDE, it can be completed automatically ):
  63. @ Suppresswarnings ("unchecked ").
  64. Make the program look like this:
  65. @ Suppresswarnings ("unchecked ")
  66. Public static void main (string [] ARGs)
  67. {
  68. List strlist = new arraylist ();
  69. Strlist. Add ("one ");
  70. Strlist. Add ("two ");
  71. Strlist. Add (New INTEGER (1 ));
  72. }
  73. In this case, I think even if you are so picky, you should be satisfied. There is neither type security nor warning. Why? The key lies in the line of the statement we added. It is also one of the new features of javatiger. annotation: As for how it is going, please pay attention to the subsequent topics.

Source: http://hi.baidu.com/qianyiliang/blog/item/431dbe223d790df6d7cae2a8.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.