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[];
2
  • 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 :-) Commented Mar 11, 2018 at 15:06
  • 1
    "Because the following crashes with an incomptaible types compilation error" both of your examples use invalid syntax anyway. Commented Mar 11, 2018 at 15:15

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.