Why foreach syntax is so ugly?
Not Joking...
Is this because of some technical problem or someone just thought of it that way?
because for each goes agains any other Java construction, for years Java has been clean to read and easy to undestand even for people that don't know the languaje.
Ironically at it may seems C#'s Foreach
foreach(type variable in Collection)
looks a lot more like Java then what they end up with
java's for each seems like C code ....
Does anyone has an explanation of why foreach is so ugly?
[540 byte] By [
elpargo] at [2007-9-30 20:31:55]

Because they didn't want to introduce another keyword that'd risk breaking legacy code.
I.e.
public class Test
{
public static void foreach(Collection c, MyOwnFunctor f)
{
Iterator i = c.iterator();
while(i.hasNext())
f.do(i.next());
}
public static void main(String[] args)
{
Set s = new TreeSet();
s.add( new Integer(1) );
s.add( new Integer(2) );
foreach(s, new PrintFunctor() ); //Would break if foreach was introduced as keyword.
}
}
Sure, the keyword "foreach" would have been nicer than "for", but the problems with new keywords mentioned above pretty much rule that out.
And do you really believe that this:
public void notifyListeners(Event event) {
for( Listener l : listeners ) l.notify(event);
}
Is harder to read than this?
public void notifyListeners(Event event) {
Iterator i = listeners.iterator();
while(i.hasNext()) {
Listener l = (Listener)i.next();
l.notify(event);
}
}