title: In Java 8, why were Arrays not given the forEach method of Iterable?
body: <p>I must be missing something here.</p>
  <p>In Java 5, the <a href="http://docs.oracle.com/javase/1.5.0/docs/guide/language/foreach.html" rel="nofollow noreferrer">&quot;for-each loop&quot; statement (also called the enhanced for loop)</a> was introduced. It appears that it was introduced mainly to iterate through <em>Collections</em>. Any collection (or container) class that implements the <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/Iterable.html" rel="nofollow noreferrer"><code>Iterable</code></a> interface is eligible for iteration using the &quot;for-each loop&quot;. Perhaps for historic reasons, the Java arrays did not implement the <code>Iterable</code> interface. But since arrays were/are ubiquitous, <code>javac</code> would accept the use of for-each loop on arrays (generating bytecode equivalent to a traditional for loop).</p>
  <p>In Java 8, the <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/Iterable.html#forEach-java.util.function.Consumer-" rel="nofollow noreferrer"><code>forEach</code> method</a> was added to the <code>Iterable</code> interface as a <em>default</em> method. This made passing lambda expressions to collections (while iterating) possible (e.g. <code>list.forEach(System.out::println)</code>). But again, arrays don't enjoy this treatment. (I understand that there are workarounds).</p>
  <p>Are there technical reasons why <code>javac</code> couldn't be enhanced to accept arrays in <code>forEach</code>, just like it accepts them in the enhanced for loop? It appears that code generation would be possible without requiring that arrays implement <code>Iterable</code>. Am I being naive?</p>
  <p>This is especially important for a newcomer to the language who rather naturally uses arrays because of their syntactical ease. It's hardly natural to switch to Lists and use <code>Arrays.asList(1, 2, 3)</code>.</p>