From the Java language specification (14.14.1):
The basic for statement executes some initialization code, then executes an
Expression, a Statement, and some update code repeatedly until the value of the
Expression is false.
The Expression is i < flowers.size() in your first example and it is evaluated once in every iteration. In your special case it shouldn't make a noteable difference, because flowers.getSize() on ArrayList is a very short method. But, in general, if the result of the expression is the same for each iteration and expensive, then do a pre calculation.
Consequence: this has to produce the same output in every implementation of a Java virtual machine and proves, that Expression is evaluate once in each iteration:
int counter2 = 10;
for (int counter1 = 0; counter1 < counter2; counter1++) {
System.out.println(counter1 + ", " + counter2);
counter2--;
}
Output:
0, 10
1, 9
2, 8
3, 7
4, 6
answered 2011-05-23T06:46:11.917