Why does giving explicit type arguments to a non-generic method or constructor compile?

前端 未结 3 2174
面向向阳花
面向向阳花 2020-12-13 19:07

When instantiating ArrayLists I am used to seeing code like this

ArrayList arr = new ArrayList();

or



        
3条回答
  •  死守一世寂寞
    2020-12-13 19:45

    I've just tried:

    ArrayList arr = new ArrayList();
    

    And got the same warning (not an error!). Looks like the compiler ignores1 the generics after the new keyword and before the ArrayList. It's just like writing:

    ArrayList arr = new ArrayList();
    

    1 I'm not sure if it really "ignores" that, I'll be glad if someone confirms/correct me

提交回复
热议问题