0

As per the java documentation says :

The Iterable<T> allows an object to be used as the target of a for-each statement.

In java we can do the following :

for (final <? super T> : T[]) {...}

In this case, why java cannot consider an object of type T[] as being an Iterable, Because the following crashes with an incomptaible types compilation error :

final Iterable<T> anIterable = T[];
marsouf
  • 1,107
  • 8
  • 15
  • 2
    "crashing" is a term that you use for severe crash situations at runtime. A compiler giving you an error message is not crashing. Being a good programmer is about being aware of such things :-) – GhostCat Mar 11 '18 at 15:06
  • 1
    "Because the following crashes with an incomptaible types compilation error" both of your examples use invalid syntax anyway. – Andy Turner Mar 11 '18 at 15:15

1 Answers1

5

An Iterable is an object all unto itself. An array is an object all unto itself. These two things are not the same object or the same type or the same anything. They have literally no relationship - hierarchial or otherwise - between themselves.

Fortunately, the enhanced-for statement is overloaded to accept both arrays and Iterable<T>, so you don't need to fuss with this.

Makoto
  • 104,088
  • 27
  • 192
  • 230