Enhanced Java for loops anyone?

2005 Jan 17 at 02:01 » Tagged as :

PHP is becoming more like java and java is becoming more like PHP. The so called enchanced for loops in java 5 is little different from the foreach loops that you encounter in PHP and some other languages.

One wonders why the new version of the for loop simply didn't follow the PHP syntax and chose a new syntax instead. You might be quick to point out the PHP is not strongly typed, that's just what makes it easier to use.

This is all about writing:

int a[];

...

int sum = 0;

for (int i=0; i

as:

int a[];

...

int sum = 0;

for (int e : a)

sum += e;

It's not fewer lines of code, but it certainly is shorter, does it really save you time? not really. Many IDEs have code templates (aka boilerplates) which you can just insert into your source with a couple of keystrokes.