0

The following code is from Arrays class:

http://developer.classpath.org/doc/java/util/Arrays-source.html

  public static <T,U> T[] copyOfRange(U[] original, int from, int to,
                      Class<? extends T[]> newType)
  {
    if (from > to)
      throw new IllegalArgumentException("The initial index is after " +
                     "the final index.");
    T[] newArray = (T[]) Array.newInstance(newType.getComponentType(),
                       to - from);
    if (to > original.length)
      {
    System.arraycopy(original, from, newArray, 0,
             original.length - from);
    fill(newArray, original.length, newArray.length, null);
      }
    else
      System.arraycopy(original, from, newArray, 0, to - from);
    return newArray;
  }

I'm baffled by the Class<? extends T[]> part, how can T[] be extended? As far as I know there's no such array like K[] that is subtype of T[] even though K extends T.

mzoz
  • 1,273
  • 1
  • 14
  • 28

1 Answers1

1

It is because unlike other objects, arrays in java are covariant. As to why arrays have this special property, I am not sure but the question has been answered on SO here .

tdct
  • 136
  • 1
  • 5