Type safety: The method.. belongs to the raw type.. refs to generics should be parameterized

前端 未结 1 1400
旧巷少年郎
旧巷少年郎 2020-12-20 17:15

Eclipse gives me the warning (in the title) using just the following code in a working project with nothing in it but a dummy class and a main method:

List a         


        
相关标签:
1条回答
  • 2020-12-20 17:42

    You have declared List a without the type parameter. This is why eclipse is complaining about type safety, as you could add objects of any type to that list.

    If you look at the ArrayList api and take a look at the class declaration, you see it is declared like this:

    public class ArrayList<E>
    

    Substitute E with any class you wish.

    With List<Integer> b you have explicitly told the compiler that the list is to hold instances of Integer objects only, and the compiler can verify this, thus giving you type safety.

    0 讨论(0)
提交回复
热议问题