Laiq Ahmed wrote:I still dont know how extended for loop works for arrays since they dont implement iterable.
Read Ankit's post again.
Ankit Garg wrote:You can iterate over arrays or anything that implements java.lang.Iterable...
Note the
word in bold - I added the bold, but the word was there already. Arrays and Iterables are two different cases, and Sun made it so you could loop over either one. You can loop through arrays because the folks at Sun decided
you should be able to loop over arrays, since it's an extremely common case. They write the compiler, and the JVM, so they simply coded it so looping through arrays with enhanced for would work. They can do that.
If they were desigining (or redesigning) Java
today, I suspect there's a good chance they would have made arrays implement Iterable. Well, if they had arrays at all - they're kind of redundant if you also have Lists. But Java was designed long ago, and some decisions were made long ago that may seem questionable now. Yet it's too late for Sun or Oracle to simply change these old decisions without creating a lot of problems for programmers trying to maintain existing software. So they don't - usually.