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

前端 未结 3 2178
面向向阳花
面向向阳花 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:22

    The code, it does nothing!

    int a = new Integer(5);
    

    It also compiles but generates a warning of "unused generics".

    So basically it is useless but not bad enough to generate an error by default it seems. Either way your arraylist is NOT properly generified here.

    Please note that generics are compiled away anyway so at the bytecode level it probably won't look any different. I have looked in eclipse for a way to turn this into an error instead of warning but no luck there.

    UPDATE

    This answer boils down to the same as the other one which is currently at +5, so why is mine downvoted? Please leave a comment if you downvote.

提交回复
热议问题