Tuesday, January 25, 2011

Foreach loop in Java 5

Here we will see new Java 5 feature foreach loop.

This is nothing but the simplified version of for loop.

Internally it will get converted to the traditional for loop.

class ForEachTest{
public static void main(String[] args){
/*
for(int i = 0; i <>
System.out.println("Hello " + args[i]);
}
*/

for(String name : args){
System.out.println("Hello " + name);
}
}
}



To compile:

javac ForEachTest.java

To run:

java ForEachTest

No comments: