This post explains that foreach loop directly corresponds to using iterator. If I write a foreach loop is it literally going to be transformed into for with iterator? In particular, given loop:
for(Integer i : createList()){
System.out.println(i);
}
Am I guaranteed to always call createList()
only once no matter what? Is it rewritten as:
for(Iterator<Integer> i = createList().iterator(); i.hasNext(); ) {
System.out.println(i.next());
}
in some kind of intermediate step or just happens to produce the same bytecode as the above?