Saturday, May 21, 2016

Enhance for loop in Java part-1


Friends today i am going to explain about enhance for loop/ for each loop. As a name it is a next to for loop. In for loop we user to iterate an array or object based on index or some condition. But in case of enhance for loop we need not to do that( or we can say that we can't do that). Enhance for loop internally uses these index or condition to iterate the array/ object.

Basically there is no concept like enhance for loop in Java for JVM. Actually this enhance for loop is only for the compiler. Compiler only understands enhance for loop and convert it into simple for loop before generation byte code. So if you see the byte code then you haven't find any enhance for loop there. For example:  If we are writing a java code fro iterating an array using enhance for loop then that is look like this:





But When we will decompile this class file then we will get like this:




So friends, this how enhance for loop works. We all know that we can use object also in enhance for loop. In next post i will write about how enhance for loop works with objects. For watching full enhance for loop video on you-tube Click here

No comments:

Post a Comment