JDK-5063030 : " T[] Collection.toArray(T[])" should be " T[] Coll
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.util:collections
  • Affected Version: 5.0
  • Priority: P3
  • Status: Closed
  • Resolution: Not an Issue
  • OS: windows_xp
  • CPU: generic
  • Submitted: 2004-06-15
  • Updated: 2017-12-08
  • Resolved: 2004-06-15
Related Reports
Relates :  
Description
I might be missing something, but the current signature of Collection.toArray(T[] a) allows the following incorrect code:

  Collection<Integer> l = ...;
  l.toArray( new Long[0] );

Changing the method signature to <T super E> would make the above code illegal,
while allowing all the valid uses legal.

###@###.### 2004-06-14
###@###.### 2004-06-14

Comments
PUBLIC COMMENTS ...
19-09-2004

EVALUATION because arrays are covariant, the proposed signature doesn't allow us to catch any additional errors at compile time. Specificaly, consider Collection<String> c = ...; Integer[] ia = new Integer[c.size()]; c.toArray(ia); this would be allowed under the proposed API change; T would be inferred as Object. This works because the Integer arrat can be received as an Object array. In short, the proposed signature is no more or less strong than the existing one, it is just more complicated. ###@###.### 2004-06-14
14-06-2004