1.Object is the parent class of all objects. 2. When the container class does not know what type will be put in, the implementation design can only use an Object array to store elements. 3. Generics are erased after compilation. In other words, for the JVM, what he sees is Object. So only Object array can be returned. As for adding generics, the compiler will help you with casts and element detection.
1.Object is the parent class of all objects.
2. When the container class does not know what type will be put in, the implementation design can only use an Object array to store elements.
3. Generics are erased after compilation. In other words, for the JVM, what he sees is Object.
So only Object array can be returned. As for adding generics, the compiler will help you with casts and element detection.
Yeah, weird.
So it is recommended to use Collection.toArray(T[] a) to return generics to avoid the problem of forced conversion of Object[].
Because we don’t know the specific type of elements in the collection. And Object is the parent class of all classes. This is more reasonable.