Mistake in the Javadoc for “Iterable”

Java has the keyword “for” to do “for each” semantics, which I always thought was strange. I mean

for ( ... ; ... ; ... )  { .. }  # standard C stuff
for (Object x : list) { ... }    # new "for each" syntax

I mean they are two different things, most languages use the keyword “foreach” for the second. I think “foreach” is more readable. I don’t see what Java stands to gain by “overloading” the “for” keyword.

Anyway, the second form takes either an array or an “iterable”. Check out the documentation for the “iterable” interface. It is succinct, but wrong.

http://docs.oracle.com/javase/7/docs/api/java/lang/Iterable.html

I conclude from this that they weren’t quite sure themselves how to name the keyword, and that they made their decision quite late in the day.

3 Responses to “Mistake in the Javadoc for “Iterable””

  1. Robin Salih Says:

    Some classic documentation fail there. I don’t expect it will be fixed soon. Have you contacted Oracle?

  2. Christian Says:

    Ha! Don’t say our high-noon discussion has inspired you! :-)

    It should really be “foreach”. It’s really two totally different concepts.

    I also find it somewhat itchy that the “foreach” application of “for” works directly with the Iterable interface. Language features that depend on implementation details of the library are somewhat weird. I mean, it’s not really bad, just a bit…. well, itchy.

  3. Lucian Izvor Says:

    There are other programming languages that use the “for” structure for a “foreach” behavior for example
    – JavaScript [ for (var key in object) ]
    – Matlab, Python but they don’t have a classic for structure
    So Java is not that special with the decision to use a simple “for”.
    Anyway looks like in Java 8 with the addition of Lambda Expressions a “forEach” method will be added to the Iterable interface that will have this form void forEach(Block block)
    A small example would be blocks.forEach(b -> { b.setColor(RED); });

    For the previous example and more info about this new feature go to :
    http://cr.openjdk.java.net/~briangoetz/lambda/collections-overview.html

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

For inserting HTML or XML please remember to use &lt; instead of <