I had an argument with my friend regarding this. Consider the below snippet,
for(i=0; i<someList.size(); i++) {
//some logic
}
Here someList.size()
will be executed for every iteration, so it is recommended to migrate this size calculation to outside(before) the loop.
Now what happens when I use an extended for loop like this,
for(SpecialBean bean: someBean.getSpecialList()) {
//some logic
}
Is it necessary to move someBean.getSpecialList()
to outside the loop?
How many times will someBean.getSpecialList()
execute if I were to retain the 2nd snippet as it is?