The for each loop

The for each loop

What is a forEach loop?

The syntaxic sugar to iterate over an iterable, internally the compiler creates an iterator for you and use it to traverse the iterable.


This syntax works for any iterable that includes all arrays and collection.


This iterator object is not an overhead, the JVM can quickly remove it through its escape analysis optimization. No one can get a reference to this iterator.?


If your object is already an iterator, you can use its forEach method and pass your consumer to it. The forEach() method of an iterable uses forEach loop internally.


You can support my work by subscribing to my YouTube channel below.

https://lnkd.in/dV3zYhTM

要查看或添加评论,请登录

社区洞察

其他会员也浏览了