Say I have a loop like
for (Foo aType : bar.getAList()) {
if (aType.getBaz().equals(baz)) {
return aType;
}
}
bar.getAList() is called on each iteration of the loop. Does the JVM/compiler keep track of the fact that this is the same object, or does it naively call the method and create a reference to the List each time?
Additionally, is this anything at all to worry about (recalling Jackson, Knuth, and Wulf’s aphorisms regarding optimization)?
bar.getAList() isn’t called in each iteration. The code is simplified into this during compilation :
Resources :