By Alvin Alexander. Last updated: June 4, 2016
It has taken me a little while to get used to the new Java for loop (foreach loop) syntax, but I've gotten a lot better with it by trying to read my for loops aloud. For instance, I would read the Java for loop below as "For each Pizza in the list of pizzas":
ListpizzaList = getPizzas();
for (Pizza pizza : pizzaList)
{
// do something with the current "pizza" reference
}
This simple trick of reading a Java for loop out loud has helped me to memorize the new syntax. Before doing this I could never remember what the for loop syntax was supposed to look like, but now I do it all the time with no problem, and yes, I do find my code much more readable than it was with the previously required Iterator syntax.