ArrayList Generic without Type

后端 未结 5 1016
执念已碎
执念已碎 2020-12-03 08:32

recently I read a piece of code which seems weird to me. As we know, we need to initialize the generic type in collections when we need to use them. Also, we know Collection

5条回答
  •  有刺的猬
    2020-12-03 09:03

    Generics were added to Java only in Java 5. Before that, when you use a collection, it always meant collection of objects. The old syntax is left as is for backward compatibility. So ArrayList result = new ArrayList() is actually creating an ArrayList. Since ArrayList is also an object, you can add content to the variable result.

    提交回复
    热议问题