-
Bug
-
Resolution: Fixed
-
P4
-
None
-
b35
-
Not verified
Collection.toArray() is specified to return an object of type Object[], but since arrays are covariant, that requirement is met by any array of a subtype of Object, for example, String[].
It's subtle, but it's clearly, if not explicitly, a bug to return an array of a subtype. SeeJDK-6260652.
Interestingly, the specification for toArray(T[]) says
Note that toArray(new Object[0]) is identical in function to toArray().
which is a fine statement, but it's in the wrong place. There should be a requirement in the toArray() spec that it returns an instance whose runtime type is precisely Object[].
It's subtle, but it's clearly, if not explicitly, a bug to return an array of a subtype. See
Interestingly, the specification for toArray(T[]) says
Note that toArray(new Object[0]) is identical in function to toArray().
which is a fine statement, but it's in the wrong place. There should be a requirement in the toArray() spec that it returns an instance whose runtime type is precisely Object[].
- csr for
-
JDK-8192841 Collection.toArray() spec should be explicit about returning precisely an Object[]
-
- Closed
-
- relates to
-
JDK-6260652 (coll) Arrays.asList(x).toArray().getClass() should be Object[].class
-
- Closed
-
-
JDK-8015667 Stream.toArray(IntFunction) ArrayStoreException should refer to component type of array
-
- Closed
-