Does Java’s for-each call an embedded method (that returns the collection) for every iteration?

No, it won’t. The result of the first call will be stored in the compiled code in a temporary variable.

From Effective Java 2nd. Ed., Item 46:

Note that there is no performance penalty for using
the for-each loop, even for arrays. In fact, it may offer a slight performance advantage
over an ordinary for loop in some circumstances, as it computes the limit of
the array index only once.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)