Wednesday, 17 February 2016

How does Enhanced for loop works in Java?

It's a long time since JDK 1.5 introduced the enhanced for loop, almost 12 years ago in the year 2004, but still many Java developers don't know basics of enhanced for loop, also known as for each loop in Java. The enhanced for loop provides the cleanest way to loop through an array or collection in Java, as you don't need to keep track of counter or you don't need to call the hasNext() method to check whether there are more elements left. The key point, which many Java developer doesn't know about the enhanced for loop is that you can use any Object which implements the Iterable interface on for (: ) construct, you can even use enhanced for loop to loop through the array. Since JDK 8 also introduced a forEach() function to provide looping functionality, don't confuse when I say for each loop with that. In this article, we'll see how enhanced for loop works and couple of important points related to it.
Read more »

No comments:

Post a Comment